Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Press key when hovering over a button to activate?

Basically what i want to do is when my mouse is hovering over a TextButton i can press a key and it'll detect it. How would i go about doing this? Heres the code i wrote but it doesn't work:

button.MouseMoved:Connect(function()
	mouse.KeyDown:Connect(function(input)
		print(input)
		if input.KeyCode==Enum.KeyCode['F'] and button.favorite.Visible==false then
			button.favorite.Visible=true
		else
			button.favorite.Visible=false
		end
	end)
end)

1 comment

0

KeyDown is deprecated, please practice UserInputService.

1 answer

So event listeners like Hover and KeyDown activate no matter where they are when you hit the key. So you need to put a debounce. Which is a fancy way of saying make a bool variable = true when you are hovering and only run the code inside KeyDown when it’s true. Also I believe there is a mouseHover event you can use instead of MouseMoved. Not sure but you can look into that.

local variableName = false
button.MouseMoved:Connect(function()
    variableName = true
    mouse.KeyDown:Connect(function(input)
        if variableName then 
        print(input)
             if input.KeyCode==Enum.KeyCode['F'] and button.favorite.Visible==false then
                 button.favorite.Visible=true
             else
                 button.favorite.Visible=false
              end
              variableName = false
        end 
    end)
end)

5 comments

0

Thank you!

0

Actually the KeyDown code runs hundreds of times and also when i stop hovering over the item. Is there anyway to fix this?

0

Nvm i forgot to set the variable back to false

0

Does it work? If it does re accept answer lol. If it doesn’t I can look at it again.

0

Also use User input service instead of KeyDown like the guy said. I won’t post the code since I’m hanging out with my family but it should be a simple fix.

This discussion is preserved as it was. New answers, comments, and votes are closed.