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

How to I add a cooldown to this code (I’m a beginner so I don’t know how)?

Asked by 2 years ago

local Tool = script.Parent local Animation = Tool.Animation

Tool.Activated:Connect(function() local Character = Tool.Parent local Humanoid = Character.Humanoid

local AnimationTrack = Humanoid:LoadAnimation(Animation)
AnimationTrack:Play()

end)

2 answers

Log in to vote
0
Answered by 2 years ago

and have it in a while loop

while true do
local AnimationTrack = Humanoid:LoadAnimation(Animation)
AnimationTrack:Play()
wait(2)
0
I meant have wait(2) within a while loop and you would also need "end)" after the wait(2) preston86542279 7 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

Add a new variable called "debounce" or any name you want, and make sure it's a boolean! A boolean is true, false, or nil. nil means nothing; nil is also false. Make sure that variable is set to false so that it will be

local debounce = false

And to make our cooldown we will check if debounce is false. It can be either if debounce == false then or if not debounce then because they will show the same result. But for this one, we'll use if debounce == false then. So it will be

Tool.Activated:Connect(function()
    if debounce == false then
        -- code here
    end
end)

Then, we'll set the debounce to true, and after the animation is finished, we'll set the debounce to true again.

Tool.Activated:Connect(function()
    if debounce == false then
        debounce = true
        task.wait(AnimationTrack.Length)
        debounce = false
    end
end)

Also, I recommend loading the animation in the animator (inside the humanoid) instead. And now, the script should look like this:

local Tool = script.Parent
local Animation = Tool.Animation

Tool.Activated:Connect(function()
    local Character = Tool.Parent
    local Humanoid = Character.Humanoid

    if debounce == false then
        debounce = true

        local AnimationTrack = Humanoid:WaitForChild("Animator"):LoadAnimation(Animation)
        AnimationTrack.Priority = Enum.AnimationPriority.Action
        repeat task.wait() until AnimationTrack.Length > 0
        AnimationTrack:Play()

        task.wait(AnimationTrack.Length)
        debounce = false
    end
end)

That's all. Let me know if there are any errors or anything that you don't understand. Have a great day! :D

Answer this question