How do I make it stop the animation when it is unequipped? Please rewrite my script because it is messy, but you don't have too. Thanks for reading.
local animation = Instance.new('Animation') local debounce = false function onEquip() script.Parent.Handle.UnsheathSound:Play() end function onActivate() if not debounce then debounce = true script.Parent.Handle.SlashSound:Play() animation.AnimationId = "http://www.roblox.com/asset/?id=129967390" local player = game.Players.LocalPlayer.Character local animTrack = player.Humanoid:LoadAnimation(animation) animTrack:Play() script.Parent.Handle.Script.Disabled = false wait(0.5) animTrack:Stop() script.Parent.Handle.Script.Disabled = true wait(0.5) debounce = false end end script.Parent.Equipped:connect(onEquip) script.Parent.Activated:connect(onActivate)
It's extremely simple, make it so the animTrack
can be accessed across more than one function. We do this by declaring it outside the function onActivate()
. Then we create a function to get when the tool is unequipped. Then we use :Stop()
on animTrack
.
All together now
local animation = Instance.new('Animation') local debounce = false local animTrack function onEquip() script.Parent.Handle.UnsheathSound:Play() end function onActivate() if not debounce then debounce = true script.Parent.Handle.SlashSound:Play() animation.AnimationId = "http://www.roblox.com/asset/?id=129967390" local player = game.Players.LocalPlayer.Character animTrack = player.Humanoid:LoadAnimation(animation) animTrack:Play() script.Parent.Handle.Script.Disabled = false wait(0.5) animTrack:Stop() script.Parent.Handle.Script.Disabled = true wait(0.5) debounce = false end end function onUnequip() animTrack:Stop() end script.Parent.Equipped:connect(onEquip) script.Parent.Unequipped:connect(onUnequip) script.Parent.Activated:connect(onActivate)
Hope this helps you.