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

Why is this debounce cooldown for my animation not working?

Asked by 5 years ago

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

Log in to vote
0
Answered by 5 years ago

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)
Ad

Answer this question