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

How To Not Have The Players Spam A Keydown/UserInput Function?

So this is a pretty basic question, and I've looked at all the sources I could but couldn't really get a direct answer, so I'll ask here: how do I make a KeyDown()/UserInputService() that the players can't spam? For example

My Example

Let's say that you made a code that the player presses a certain key on their keyboard and an action happens, how would I make it so that way they can't spam the function? Something like this

function onKeyDown(input)
local key = input.KeyCode
if key == Enum.KeyCode.Z then
--function
end
game:GetService('UserInputService').InputBegan:connect(onKeyDown)

So..

Can you guys help me based off my example? Thanks for looking at my question, have a good day.

Greek

3 comments

1

try adding a debounce, works for me. like "if key == Enum.KeyCode.Z and debounce == false then debounce = true end"

0

Intresting, I'll try it out.

1 answer

✓ Accepted answer
local Ready = true
function onKeyDown(input)
local key = input.KeyCode
if key == Enum.KeyCode.Z and Ready == true then
Ready = false
-- Put code here
wait(1)
Ready = true
end
game:GetService('UserInputService').InputBegan:connect(onKeyDown)


2 comments

0

Use debounce to reduce spam

1

Thanks for that, yours was the only answer and it was correct so I accepted it. Thanks for the help!

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