Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Please help me! I'm having trouble with animations!?

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!

2 comments

0

Please put your code in a code block please. Just click the blue Lua button and put your code there. Is should automatically format it for the most part. It makes it much easier to help you.

0

Try animation track:Stop(). If that doesn't work wait for someone smarter than me.

1 answer

An Easy Solution

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

A Better Solution

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.