Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to add cooldown to Animation?

Asked by 4 years ago

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

Log in to vote
0
Answered by 4 years ago

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!

0
Didn't quite work. Only delays the animation and is still spammable LargeMakesStuff 2 — 4y
0
Use debounce. killerbrenden 1537 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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.

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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!

Answer this question