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 add a cooldown to this tool?

Hello! I am new to scripting, and I was wondering how I can add a cooldown to this tool. It's not complete, it's supposed to make the player switch between their civillian costume and superhero costume, as well as giving their powers. So I don't really want it to be spammed. Thank you!

script.Parent.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
		local animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
		script.Parent.Handle.Sparkles.Enabled = true
		
		wait (1)
		
		animation:Play()
		wait (2)
		script.Parent.Handle.Sparkles.Enabled = false
		
	end)
end)


1 comment

0

try using an a Tool.Activated event instead of a combination of mouse.Button1Down and Tool.Equipped

1 answer

✓ Accepted answer

You would add a Debounce to prevent the code from running during the cooldown.

Example

local deb = false

script.Parent.Equipped:Connect(function(Mouse)
    Mouse.Button1Down:Connect(function()
	if deb then return end -- exit code if deb is true
	deb = true

        local animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
        script.Parent.Handle.Sparkles.Enabled = true

        wait (1)

        animation:Play()
        wait (2)
        script.Parent.Handle.Sparkles.Enabled = false
	
	deb = false -- reset after wait
    end)
end)

Hope this helps.

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