Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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

1 comment

0

Thank you very much!

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

	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

This discussion is preserved as it was. New answers, comments, and votes are closed.