How should i add a cooldown to this?

basically, the tool swings without delay if you click fast enough, but in the code i added a debounce that only works if you dont spam click, so how should i add a cooldown that actually work:

CODE(server script) :

Bat.Activated:Connect(function()
		if not Debounce then
	
	if Swings == 0 then
			SwingRight:Play()
				Swings = 1
				task.wait(.35)
				newHitbox:HitStart()
				Swing1Sound:Play()
	elseif Swings == 1 then
			SwingLeft:Play()
				Swings = 2
				task.wait(.35)
				newHitbox:HitStart()
				Swing1Sound:Play()
	elseif Swings == 2 then
			SwingOverHead:Play()
				Swings = 0
				task.wait(.25)
				newHitbox:HitStart()
				Swing2Sound:Play()
			end
		end
		Debounce = true
		task.wait(Cooldown)
		Debounce = false
end)

Add the debounce above the if statements, currently it will wait depending on the if statement it goes into before turning on the debounce.
EX:

		if not Debounce then
			Debounce = true
	if Swings == 0 then
			SwingRight:Play()
				Swings = 1
				task.wait(.35)
				newHitbox:HitStart()
				Swing1Sound:Play()
	elseif Swings == 1 then
			SwingLeft:Play()
				Swings = 2
				task.wait(.35)
				newHitbox:HitStart()
				Swing1Sound:Play()
	elseif Swings == 2 then
			SwingOverHead:Play()
				Swings = 0
				task.wait(.25)
				newHitbox:HitStart()
				Swing2Sound:Play()
			end
		end
		task.wait(Cooldown)
		Debounce = false
end)```
1 Like

the first swing works, but after that you can still spam click to bypass it, so same result.

Where is the variable Cooldown (3rd last line) set? You may have an issue with that code instead of this one.
wait(.25) or wait(.35) isn’t very long at all, so is the spamming only waiting that long?
Try printing Cooldown to see what it’s doing.

it set at the top, like this: local Cooldown = 3, the cool down works (like i stated before) but if you spam activate (or click) the tool, the debounce just wont work in general. (it prints 3)

Is it still proceeding through all 3 swings when you spam click?

yeah, all 3 swings play while bypassing the debounce by spam clicking

What happens if you use @Btkelley17’s script and just put the number 3 instead of Cooldown?

Try this…

local debounce = false

if debounce == false then
   debounce = true

    if Swings == 0 then
        SwingRight:Play()
        Swings = 1
        task.wait(.35)
        newHitbox:HitStart()
        Swing1Sound:Play()
    elseif Swings == 1 then
        SwingLeft:Play()
        Swings = 2
        task.wait(.35)
        newHitbox:HitStart()
        Swing1Sound:Play()
    elseif Swings == 2 then
        SwingOverHead:Play()
        Swings = 0
        task.wait(.25)
        newHitbox:HitStart()
        Swing2Sound:Play()
    end

    wait() --enter cooldown here
    debounce = false
end
1 Like

i am using his script, and that wont change anything, remember the debounce works, but is bypassed by spam clicking

is it because the debounce = true had to be right after the if statement? or because i used if not debounce instead of if debounce == false? hmmm.

if not debounce should work, though you need to filter the debounce as soon as possible. You filtered your debounce 0.35 seconds after it is called, meaning that requests can be made within that amount of time. :+1:

1 Like