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.

01local debounce = true
02script.Parent.Equipped:Connect(function(Mouse)
03    Mouse.Button1Down:Connect(function()
04        if debounce == true then
05            debounce = false
06 
07        animation = game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):LoadAnimation(script.Parent.Animation)
08        animation:Play()
09        wait(1.5)
10        debounce = true
11        end
12    end)
13end)
14 
15script.Parent.Unequipped:Connect(function()
16        animation:Stop()
17debounce = false
18end)

i am having issues specifically with this part of the script

1script.Parent.Unequipped:Connect(function()
2        animation:Stop()
3debounce = false
4end)

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.

01local debounce = true
02local animation
03script.Parent.Equipped:Connect(function(Mouse)
04    Mouse.Button1Down:Connect(function()
05        if debounce == true then
06            debounce = false
07 
08        animation = game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):LoadAnimation(script.Parent.Animation)
09        animation:Play()
10        wait(1.5)
11        debounce = true
12        end
13    end)
14end)
15 
16script.Parent.Unequipped:Connect(function()
17        animation:Stop()
18debounce = false
19end)
Ad

Answer this question