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

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

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

✓ Accepted answer

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)

6 comments

0

For some reason this didn't work. Is there another way? I did the same thing you did and looked over it.

0

Try increasing the wait() to 10.

0

Still doesn't work.

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

0

Fixed, try now. Thank you vector3_zero.

0

Thanks it worked.

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