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

Animation in tool keeps breaking after it is unequiped?

Asked by 4 years ago

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

Log in to vote
1
Answered by
uhSaxlra 181
4 years ago

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

Answer this question