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

How do you stop an already playing animation when you unequip a tool?

Asked by 7 years ago

I have an idle animation that plays when a tool is equipped. For example, say that the tool is a gun; when I equip the gun, I have the character play an idle gun holding animation. This animation is set to looped, so it plays forever, and I want to make it so that it stops playing when the gun is unequipped. However, I don't think I'm getting anywhere close:

local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:FindFirstChild("Humanoid")
local mouse = player:GetMouse()

local weapon = script.Parent
local holdanimation = Instance.new("Animation")
holdanimation.Name = "HoldingAnimation"
holdanimation.AnimationId =  "http://www.roblox.com/Asset?ID=744566899"

weapon.Equipped:connect(function(mouse)
    local animation = humanoid:LoadAnimation(holdanimation)
    animation:Play()
end)

weapon.Unequipped:connect(function()
    local animation = humanoid:GetPlayingAnimationTracks()
    local n = 0
    repeat 
        if animation[n].Name == "HoldingAnimation" then --Finding the animation and stopping it?
            animation[n]:Stop()
            n = animation.Length
        end
        n = n + 1
    until n > animation.Length
end)

Any help would be much appreciated.

Answer this question