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

Animation doesnt stop when the tool is Unequipped?

Asked by 6 years ago
Edited 6 years ago

I have a tool and its got some animation and when you unequip the tool its supposed to stop the animation but it doesnt.

Heres the code, its inside a script not a local script

local CanAnimate = true

script.Parent.Equipped:Connect(function()
    AttackAnimation = script.parent.parent.Humanoid:LoadAnimation(script.Parent.Handle.AttackAnimation)
    IdleAnimation = script.parent.parent.Humanoid:LoadAnimation(script.Parent.Handle.IdleAnimation)

    IdleAnimation:Play()
end)

script.Parent.Activated:Connect(function()
    if CanAnimate == true then
        AttackAnimation = script.parent.parent.Humanoid:LoadAnimation(script.Parent.Handle.AttackAnimation)
        IdleAnimation = script.parent.parent.Humanoid:LoadAnimation(script.Parent.Handle.IdleAnimation)

        CanAnimate = false
        script.Parent.CanAttack.Value = true
        IdleAnimation:Stop()
        AttackAnimation:Play()
        wait(1)
        IdleAnimation:Play()
        CanAnimate = true

    end
end)

script.Parent.Unequipped:Connect(function()
    IdleAnimation = script.parent.parent.parent.Character.Humanoid:LoadAnimation(script.Parent.Handle.IdleAnimation)

    IdleAnimation:Stop()
end)

Thank you for your time, I know its messy with the all the ".parents" everywhere but thats because ive been experimenting trying to get it working. So far it doesnt error but it also doesnt stop the animation.

Thank you for your time :)

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

I would structure that a little differently.

local CanAnimate = true
AttackAnimation = nil
IdleAnimation = nil

script.Parent.Equipped:Connect(function()
    AttackAnimation = script.parent.parent.Humanoid:LoadAnimation(script.Parent.Handle.AttackAnimation)
    IdleAnimation = script.parent.parent.Humanoid:LoadAnimation(script.Parent.Handle.IdleAnimation)

    IdleAnimation:Play()
end)

script.Parent.Activated:Connect(function()
    if CanAnimate == true then 
        CanAnimate = false
        script.Parent.CanAttack.Value = true
        IdleAnimation:Stop()
        AttackAnimation:Play()
        wait(1)
        IdleAnimation:Play()
        CanAnimate = true
    end
end)

script.Parent.Unequipped:Connect(function()
    AttackAnimation:Stop() -- Clean up
    IdleAnimation:Stop()
    AttackAnimation = nil
    IdleAnimation = nil
end)

I think your problem is that you are creating a new AnimationTrack everytime so the track currently playing doesnt get stopped. So what I did here was make it so the animation only loads when equipped then is destroyed when you unequip.

0
Thanksss GottaHaveAFunTime 218 — 6y
Ad

Answer this question