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

How do I make a cool down for my key press remote event?

Asked by
Zvtu 4
4 years ago

I am not good at all at scripting and really need to know how to make cool downs. How do you make a cooldown for this?

Code:

local UserInputService = game:GetService("UserInputService")  UserInputService.InputBegan:Connect(function(input)   
    if input.KeyCode == Enum.KeyCode.F then              
        game.ReplicatedStorage.TimeStop:FireServer()    
      end 
end)

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

There are more complex and secure ways to create a cooldown, but one of the simplest would be creating a Debounce. It basically stops the event from firing unless the Debounce is set the respective value.

local OnCooldown = false -- It currently isn't on cooldown.
local UserInputService = game:GetService("UserInputService")  UserInputService.InputBegan:Connect(function(input)   
    if input.KeyCode == Enum.KeyCode.F and not OnCooldown then
    OnCooldown = true -- Setting the Cooldown to true, so it can't fire again.              
        game.ReplicatedStorage.TimeStop:FireServer()  
    wait(1) -- How long you want the Cooldown to be, I have it set to 1 second.
    OnCooldown = false -- No longer on Cooldown.
      end 
end)
0
For some reason this didn't work. Is there another way? I did the same thing you did and looked over it. Zvtu 4 — 4y
0
Try increasing the wait() to 10. JoyfulPhoenix 139 — 4y
0
Still doesn't work. Zvtu 4 — 4y
0
Between lines 3 and 4 in the above code you need to add the condition 'if OnCooldown == false then', and put the end for the if statement between lines 7 and 8 vector3_zero 1056 — 4y
View all comments (2 more)
0
Fixed, try now. Thank you vector3_zero. JoyfulPhoenix 139 — 4y
0
Thanks it worked. Zvtu 4 — 4y
Ad

Answer this question