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

Animation in tool keeps breaking after it is unequiped?

so i have this script inside a tool to play a animation when the player clicks with the tool out.

local debounce = true
script.Parent.Equipped:Connect(function(Mouse)
    Mouse.Button1Down:Connect(function()
        if debounce == true then
            debounce = false

        animation = game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):LoadAnimation(script.Parent.Animation)
        animation:Play()
        wait(1.5)
        debounce = true
        end
    end)
end)

script.Parent.Unequipped:Connect(function()
        animation:Stop()
debounce = false
end)

i am having issues specifically with this part of the script

script.Parent.Unequipped:Connect(function()
        animation:Stop()
debounce = false
end)

sometimes when you unequip and equip the tool the script will just stop working. the animation will just stop playing. any idea how to fix this?

1 answer

✓ Accepted answer

You're problem is that the function can't read the animation variable since it is local to the other function. To fix this, you can simply add a global variable that all functions can read.

local debounce = true
local animation
script.Parent.Equipped:Connect(function(Mouse)
    Mouse.Button1Down:Connect(function()
        if debounce == true then
            debounce = false

        animation = game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):LoadAnimation(script.Parent.Animation)
        animation:Play()
        wait(1.5)
        debounce = true
        end
    end)
end)

script.Parent.Unequipped:Connect(function()
        animation:Stop()
debounce = false
end)

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