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

Why does my animation not stop when a player unequipped a tool?

Asked by 5 years ago
Edited 5 years ago

When I load the game, the animations play and work properly.

But when I try to stop the animaitions it doesn't work.

local walkanim = script.Parent.Parent.Humanoid:LoadAnimation(script.Walk)
local idleanim = script.Parent.Parent.Humanoid:LoadAnimation(script.Idle)

script.Parent.Equipped:Connect(function()

    local mouse = game.Players.LocalPlayer:GetMouse()
    local player = game.Players.LocalPlayer
    local mouse = game.Players.LocalPlayer:GetMouse()
    idleanim:Play()

    mouse.KeyDown:connect(function(key)
        if key ==  "w" then
            walkanim:Play()
            idleanim:Stop()
        end
    end)
    mouse.KeyUp:connect(function(key2)
        if key2 == "w" then
            walkanim:Stop()
            idleanim:Play()
        end
    end)
end)
script.Parent.Unequipped:Connect(function()
    walkanim:Stop()
    idleanim:Stop()
end)

Any help would be nice. Thanks!

1 answer

Log in to vote
1
Answered by
Sorukan 240 Moderation Voter
5 years ago

The codes inside the equipped function will continue to run so you have to Disconnect it. You can do it by writing the function as a variable like this

local pie = script.Parent.Equipped:Connect(function()

The function will still work like normal, this will just make it so that it can be used as a variables. After you've done this, you can disconnect it when you unequip the tool so like this.

script.Parent.Unequipped:Connect(function()
    walkanim:Stop()
    idleanim:Stop()
    pie:Disconnect()
0
This works, but why does my idle animation not work when I re-equip the tool? kittonlover101 201 — 5y
Ad

Answer this question