so i have this script inside a tool to play a animation when the player clicks with the tool out.
01 | local debounce = true |
02 | script.Parent.Equipped:Connect( function (Mouse) |
03 | Mouse.Button 1 Down: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 ) |
13 | end ) |
14 |
15 | script.Parent.Unequipped:Connect( function () |
16 | animation:Stop() |
17 | debounce = false |
18 | end ) |
i am having issues specifically with this part of the script
1 | script.Parent.Unequipped:Connect( function () |
2 | animation:Stop() |
3 | debounce = false |
4 | 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?
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.
01 | local debounce = true |
02 | local animation |
03 | script.Parent.Equipped:Connect( function (Mouse) |
04 | Mouse.Button 1 Down: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 ) |
14 | end ) |
15 |
16 | script.Parent.Unequipped:Connect( function () |
17 | animation:Stop() |
18 | debounce = false |
19 | end ) |