Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

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

Asked by 7 years ago

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

1
try adding a debounce, works for me. like "if key == Enum.KeyCode.Z and debounce == false then debounce = true end" Senor_Chung 210 — 7y
0
Intresting, I'll try it out. GreekGodOfMLG 244 — 7y

1 answer

Log in to vote
1
Answered by 7 years ago
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)


0
Use debounce to reduce spam UltraUnitMode 419 — 7y
1
Thanks for that, yours was the only answer and it was correct so I accepted it. Thanks for the help! GreekGodOfMLG 244 — 7y
Ad

Answer this question