function onKeyPress(inputObject,gameProcessed) if inputObject.KeyCode == Enum.KeyCode.V then --do stuff end end game:GetService("UserInputService").InputBegan:connect(onKeyPress)-- an event for buttons on keyboard
using this event, along with a key event, only sends it once, when the inputObject is pressed. I am aiming for the user to hold down a button, but there is not options for it at all. I have tried while loops, ends up crashing, I also have tried using isKeyDown, which only tells me if the key is part of the keyboard...
One method could be to add a debounce and a while loop, for example when you press the button you change the debounce to true and when the button is released, make it false. Then all you have to do is use a while debounce == true do loop.
KeyHeld = false function onKeyPress(inputObject,gameProcessed) if inputObject.KeyCode == Enum.KeyCode.V then KeyHeld = true while KeyHeld then --do stuff wait() end end end function onKeyRelease(inputObject,gameProcessed) if inputObject.KeyCode == Enum.KeyCode.V then KeyHeld = false end end game:GetService("UserInputService").InputBegan:connect(onKeyPress) game:GetService("UserInputService").InputEnded:connect(onKeyRelease)
This is how I've been using controls for my games:
local tabDown = false local input = game:GetService("UserInputService") local player = game.Players.LocalPlayer input.InputBegan:connect(function(k) local key = k.KeyCode if key == Enum.KeyCode.Tab then tabDown = true end end) input.InputEnded:connect(function(k) local key = k.KeyCode if key == Enum.KeyCode.Tab then tabDown = false end end) while wait() do if tabDown then print("Tab is down") else print("Tab is not down") end end
It works very well for me. Of course in your case you would have to change the Enum for the key but other than that it should work.