Is this is a good way to make a cooldown?

Hey everyone I want create a tick but I am afraid of bugs and more.

This script seems like it has some problems cuz at the beginning it doesnt react:

Should I chooce another way cuz this seems like not working like I want?

local previous = tick()
    uis.InputBegan:Connect(function(input)
    	if input.KeyCode == Enum.KeyCode.A and pressA == false and tick() - Previous > CoolDown then
    		Previous = tick()
    		pressA = true 
    		print("hey")
    	end
    	if input.KeyCode == Enum.KeyCode.D and pressD == false and tick() - Previous > CoolDown then
    		Previous = tick()
    		pressD = true 
    		print("hey")
    	end
    	if input.KeyCode == Enum.KeyCode.S and pressS == false and tick() - Previous > CoolDown then
    		Previous = tick()
    		pressS = true 
    		print("hey")
    	end
    	if input.KeyCode == Enum.KeyCode.W and pressW == false and tick() - Previous > CoolDown then
    		pressW = true 
    		Previous = tick()
    		print("hey")
    	end
    end)
if pressed == true then
 wait()-- time you want to wait
 pressed = false
end

You could do

local Cooldown = 10

-- Somewhere in the code
wait(Cooldown)

but is “waiting” not good for exploiters?

hackers can exploit my game easy with that

what do you mean???

Also I want put long Tweens in there with my inputs so why should I use a

wait()?

here’s a good denounce tutorial aka cooldown

Oh sry I missunderstood something

If it’s on server then it’s impossible to exploit since clients don’t have access to the server scripts. Also what is the exact scenario your in?

but wait what If I want it to use in RunService

You could have a variable that represents a starting time. Whenever someone tries to use whatever has a cooldown, check if the current time - the start time is greater than the wait time. If it is, allow them to use it. An example may look like this:

local currentTime = tick()
local cooldown = 3

local function doStuff()
    if tick() - currentTime >= cooldown then
        currentTime = tick()
        print("Cooldown is over")    

    end

end

3 Likes

Be careful with having your cooldowns relative to runservice if you dont use delta time. If your cooldown is decreased every frame, the higher the FPS, the faster the cooldown.

3 Likes

Like when I use it in loop or other then it will crash

while
— there happens a lot untill the debounce is false and the wait is finish
wait()
debounce = false

Can I use ur function with a if statement will it work?

1 Like

It should, but it depends on your current implementation.

doStuff already does the if stuff

1 Like

what do you mean you cant test it out in ur Studio??

Cooldown = 3
RunService.....
if debounce == true then
print("hey") -- prints(10000 times
wait(CoolDown)
debounce = false
end
end)

what???

1 Like

Your method with debounce and wait() isnt a good Method