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

How to add cooldown to Animation?

Greetings, beginner scripter here. Currently i'm making a tool that has an animation, but im having trouble finding a way to add a 0.6 second cooldown. Any way anyone can incorporate it? Thanks

script.Parent.Equipped:Connect(function(Mouse)
    Mouse.Button1Down:Connect(function()
        local animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
        animation:Play()
    end)
end)

3 answers

Here is a solution, Make a variable which is a number

local CoolDownSeconds = 0

Now make it check if that variable is 0.6 and if not then wait 0.6 seconds and change the variable

local CoolDownSeconds = 0

script.Parent.Equipped:Connect(function(Mouse)
    Mouse.Button1Down:Connect(function()
    if CoolDownSeconds == 0.6 then
        local animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
        animation:Play()
    else
    repeat
    wait(0.1)
    CoolDownSeconds = CoolDownSeconds + 0.1
    until CoolDownSeconds == 0.6
    if CoolDownSeconds == 0.6 then
    local animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
    animation:Play()
    CoolDownSeconds = 0
    end
    end
    end)
end)

If you want a shorter version then here

local CoolDownSeconds = 0

script.Parent.Equipped:Connect(function(Mouse)
    Mouse.Button1Down:Connect(function()
    repeat
    wait(0.1)
    CoolDownSeconds = CoolDownSeconds + 0.1
    until CoolDownSeconds == 0.6
    local animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
    animation:Play()
    CoolDownSeconds = 0
end)
end)

Hope I helped and happy coding!

2 comments

0

Didn't quite work. Only delays the animation and is still spammable

0

Use debounce.

Change the wait() to however long the animation is +0.75 seconds to insure that it has stopped fully. And gives the animation time to cooldown.

local debounce = true

script.Parent.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
		if debounce == true then
			debounce = false
			local animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
			animation:Play()
			wait() -- however long the animation is + 0.75 seconds. So if the animation length is 7.5, it would wait(8.25), to insure that it has been ended.
			debounce = true
		end
	end)
end)

You can really name debounce to whatever, but it's just a general thing people use.

I was actually doing this myself a while ago, but with a key instead of a mouse. Here is what I did:

local forLoop = 1
local canFlip = 1

local anim = Instance.new("Animation")
anim.AnimationId = --Put animation here

mouse.KeyDown:connect(function(key)
		if key == "f" and canFlip == 1 then
			if key == "f" then
				canFlip = 0
				local playAnim = humanoid:LoadAnimation(anim)
				playAnim:Play()
		elseif key == "f" and canFlip == 0 then
			print("Animation needs to recharge!")
		end
end)

while forLoop == 1 do
	if canFlip == 0 then
		canFlip = 1
	end
	wait(0.75) --Put your cooldown time here
end

Just change the key to the mouse, and you should be good to go. Hope this helps!

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