Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 8 years ago

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!

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. User#11440 120 — 8y
0
Try animation track:Stop(). If that doesn't work wait for someone smarter than me. User#11440 120 — 8y

1 answer

Log in to vote
0
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
8 years ago

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)
Ad

Answer this question