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

Why is this debounce cooldown for my animation not working?

What I want to do is make a 3 second cool down for each time the animation plays when I left Click.

local debounce = false

script.Parent.Equipped:Connect(function(Mouse)
    Mouse.Button1Down:Connect(function()
			if not debounce then
			debounce = true
		end

		print("work!")    
		animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
        animation:Play()

		wait(3)
		debounce = false
    end)
end)
		
script.Parent.Unequipped:Connect(function()
        animation:Stop()
end)

Help.

1 answer

Your debounce is useless if you don't have an if statement that restricts the event from passing through.


Understanding Debounces

local debounce = true

MouseButton1Click:Connect(function()
	if (debounce == true) then

		debounce = false

		print("Hello!")

	end
end)

This line of code will only print "Hello!" once because right when you click, your debounce turns false. This means when you click again it'll check if your debounce (which is false) is true.


In order to make it say "Hello!" again, we must make the debounce true again. That is also where we can add wait to prevent it from being clicked multiple times.

local debounce = true

MouseButton1Click:Connect(function()
	if (debounce == true) then

		debounce = false

		print("Hello!")

		wait(3)
		debounce = true

	end
end)

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