I have a tool and its got some animation and when you unequip the tool its supposed to stop the animation but it doesnt.
Heres the code, its inside a script not a local script
local CanAnimate = true script.Parent.Equipped:Connect(function() AttackAnimation = script.parent.parent.Humanoid:LoadAnimation(script.Parent.Handle.AttackAnimation) IdleAnimation = script.parent.parent.Humanoid:LoadAnimation(script.Parent.Handle.IdleAnimation) IdleAnimation:Play() end) script.Parent.Activated:Connect(function() if CanAnimate == true then AttackAnimation = script.parent.parent.Humanoid:LoadAnimation(script.Parent.Handle.AttackAnimation) IdleAnimation = script.parent.parent.Humanoid:LoadAnimation(script.Parent.Handle.IdleAnimation) CanAnimate = false script.Parent.CanAttack.Value = true IdleAnimation:Stop() AttackAnimation:Play() wait(1) IdleAnimation:Play() CanAnimate = true end end) script.Parent.Unequipped:Connect(function() IdleAnimation = script.parent.parent.parent.Character.Humanoid:LoadAnimation(script.Parent.Handle.IdleAnimation) IdleAnimation:Stop() end)
Thank you for your time, I know its messy with the all the ".parents" everywhere but thats because ive been experimenting trying to get it working. So far it doesnt error but it also doesnt stop the animation.
Thank you for your time :)
I would structure that a little differently.
local CanAnimate = true AttackAnimation = nil IdleAnimation = nil script.Parent.Equipped:Connect(function() AttackAnimation = script.parent.parent.Humanoid:LoadAnimation(script.Parent.Handle.AttackAnimation) IdleAnimation = script.parent.parent.Humanoid:LoadAnimation(script.Parent.Handle.IdleAnimation) IdleAnimation:Play() end) script.Parent.Activated:Connect(function() if CanAnimate == true then CanAnimate = false script.Parent.CanAttack.Value = true IdleAnimation:Stop() AttackAnimation:Play() wait(1) IdleAnimation:Play() CanAnimate = true end end) script.Parent.Unequipped:Connect(function() AttackAnimation:Stop() -- Clean up IdleAnimation:Stop() AttackAnimation = nil IdleAnimation = nil end)
I think your problem is that you are creating a new AnimationTrack everytime so the track currently playing doesnt get stopped. So what I did here was make it so the animation only loads when equipped then is destroyed when you unequip.