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

Why does my animation still play even though I unequip a tool?

Asked by 5 years ago

I have a script when a tool is equipped two animations "idle" and "run" but when I unequip the tool the animation still plays, Any help?

local plr = game.Players.LocalPlayer

IdleAnim = script:WaitForChild("Idle")
RunAnim = script:WaitForChild("Run")

script.Parent.Equipped:Connect(function()
    plr.Character.Humanoid.Running:Connect(function(value)
        if value>0 then
            playrun()
        else
            playidle()
        end
    end)
end)


script.Parent.Unequipped:Connect(function()
    local run = plr.Character.Humanoid:LoadAnimation(RunAnim)
    local idle = plr.Character.Humanoid:LoadAnimation(IdleAnim)
    run:Stop()
    idle:Stop()
end)


function playidle()
    local run = plr.Character.Humanoid:LoadAnimation(RunAnim)
    local idle = plr.Character.Humanoid:LoadAnimation(IdleAnim)
    run:Stop()
    idle:Play()
end

function playrun()
    local run = plr.Character.Humanoid:LoadAnimation(RunAnim)
    local idle = plr.Character.Humanoid:LoadAnimation(IdleAnim)
    idle:Stop()
    run:Play()
end

4 answers

Log in to vote
0
Answered by
DanzLua 2879 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

You want to create and use LoadAnimation outside of these events so both event functions has the animation.

Also you want to disconnect the running event so that you don't create it over and over again.

local plr = game.Players.LocalPlayer

local IdleAnim = script:WaitForChild("Idle")
local RunAnim = script:WaitForChild("Run")

local run = plr.Character.Humanoid:LoadAnimation(RunAnim)
local idle = plr.Character.Humanoid:LoadAnimation(IdleAnim)

local runE

script.Parent.Equipped:Connect(function()
    runE=plr.Character.Humanoid.Running:Connect(function(value)
        if value>0 then
            playrun()
        else
            playidle()
        end
    end)
end)


script.Parent.Unequipped:Connect(function()
    run:Stop()
    idle:Stop()
    if runE then
        runE:Disconnect()
        runE=nil
    end
end)


function playidle()
    run:Stop()
    idle:Play()
end

function playrun()
    idle:Stop()
    run:Play()
end
0
Thank you very much! biggun428 8 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

I can't tell you if it's the issue but i think that you need to use :Disconnect()

Log in to vote
0
Answered by 5 years ago
    local plr = game.Players.LocalPlayer
    local equiped = true
    IdleAnim = script:WaitForChild("Idle")
    RunAnim = script:WaitForChild("Run")
    local run = plr.Character.Humanoid:LoadAnimation(RunAnim)
    local idle = plr.Character.Humanoid:LoadAnimation(IdleAnim) 

    script.Parent.Equipped:Connect(function()
        equiped = true
        plr.Character.Humanoid.Running:Connect(function(value)
            if equiped == true then
            if value>0 then
                playrun()
            else 
                playidle()
            end
            end
        end)
    end)


    script.Parent.Unequipped:Connect(function()
        equiped = false
        run:Stop()
        idle:Stop()
    end)


    function playidle()    
        run:Stop()
        idle:Play()
    end

    function playrun()
        idle:Stop()
        run:Play()
    end

Log in to vote
0
Answered by 5 years ago

I think you need a Disconnect() in there

Answer this question