So here's my script...
local tool = script.Parent local animation = script.Animation local animation2 = script.Animation2 local player = game.Players.LocalPlayer
tool.Equipped:connect(function() local character = player.Character local humanoid = character:WaitForChild("Humanoid") local animationtrack = humanoid:LoadAnimation(animation) animationtrack:Play() end)
tool.Unequipped:connect(function()
end)
So as you can see, the equipped function plays an animation and this is working fine, but I want to make something where if the tool is unequipped, the animation stops and that character goes back into its default position. What do I have to put into the unequipped function to do this? Is there some property I don't know about? Help!
There is a function of Humanoid called GetPlayingAnimationTracks that returns an array of all the AnimationTrack's of the Humanoid that are being played. You can use this to call stop on each of the tracks:
for _, track in next, player.Character.Humanoid:GetPlayingAnimationTracks() do track:Stop() end
Doing this, you may find that your default character animations will stop until you move again. You can single out which animations you want to stop by storing the AnimationTracks in a table when you create them:
local Tool = script.Parent local Animation = script.Animation local Animation2 = script.Animation2 local Player = game.Players.LocalPlayer local AnimTracks = {} Tool.Equipped:connect(function() local character = Player.Character local humanoid = character:WaitForChild("Humanoid") local animationtrack = humanoid:LoadAnimation(Animation) table.insert(AnimTracks, animationtrack) animationtrack:Play() end) Tool.Unequipped:connect(function() for _, track in next, AnimTracks do track:Stop() track:Destroy() -- Because you create a new one on equip end end)