tool.Equipped playing after unequip

I am trying to stop the animation when the tool is unequipped but even after I stop it it still goes

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local RS = game:GetService("ReplicatedStorage")
local idle
local equip = humanoid:LoadAnimation(RS.Equip)
local tool = script.Parent
local active = false
local db = false
tool.Equipped:Connect(function() 

active = true
	humanoid.Running:Connect(function(speed)
		if  humanoid.MoveDirection.Magnitude == 0 and active then
			if not idle then idle = humanoid:LoadAnimation(RS.Idle) end
			if equip then  equip:Stop() end
			idle:Play()
		else
			if idle then  idle:Stop() end 
			equip:Play()
		end
	end)
end)


tool.Unequipped:Connect(function()
	equip:Stop()
   active = false
	if idle.IsPlaying then 
		idle:Stop() 
	end
	
	if equip.IsPlaying then
		equip:Stop()

	end
end)

What if instead of triggering the animation using the tool.Equipped event, why don’t you just add a check to see if the tool is currently equipped to the big if statement where you check if the humanoid is running?

You should disconnect the connection when unequipped

local connection
tool.Equipped:Connect(function() 

	active = true
	connection = humanoid.Running:Connect(function(speed)
		if  humanoid.MoveDirection.Magnitude == 0 and active == true then
			if not idle then idle = humanoid:LoadAnimation(RS.Idle) end
			if equip then  equip:Stop() end
			idle:Play()
		else
			if idle then  idle:Stop() end 
			equip:Play()
		end
	end)
end)

tool.Unequipped:Connect(function()
	connection:Disconnect()
	equip:Stop()
	active = false
	if idle.IsPlaying then 
		idle:Stop() 
	end

	if equip.IsPlaying then
		equip:Stop()
	end
end)
3 Likes

Exactly with @ClonedPart ;

A little more context with it.

Even though the connection is made inside another connect function, it will still run until it is disconnected properly.

tool.Equipped:Connect(function() --/!!/  tool equipped

	humanoid.Running:Connect(function() --/!!/  connection made
--/!!/  this will now run itself even though it's in another function, 
--/!!/  the function it's under doesn't have to be called again, this connection is in use.
	end

end)
tool.Equipped:Connect(function() --/!!/  tool equipped

	connection = humanoid.Running:Connect(function(speed) --/!!/  connection made here with running
--/!!/  runs with the connection independently, until disconnected
	end)

end)

tool.Unequipped:Connect(function() --/!!/  once tool unequipped
	connection:Disconnect() --/!!/  disconnect the connection
end)
1 Like
local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local replicated = game:GetService("ReplicatedStorage")
local animation1 = replicated:WaitForChild("Equip")
local animation2 = replicated:WaitForChild("Idle")
local track1 = animator:LoadAnimation(animation1)
local track2 = animator:LoadAnimation(animation2)

local tool = script.Parent

local connection

tool.Equipped:Connect(function()
	if connection then
		if connection.Connected then
			return
		end
	end
	
	connection = humanoid.Running:Connect(function(speed)
		if speed > 0 then
			if track2.IsPlaying then
				track2:Stop()
			end
			
			if not track1.IsPlaying then
				track1:Play()
			end
		else
			if track1.IsPlaying then
				track1:Stop()
			end
			
			if not track2.IsPlaying then
				track2:Play()
			end
		end
	end)
end)


tool.Unequipped:Connect(function()
	if connection then
		if connection.Connected then
			connection:Disconnect()
		end
	end
	
	track1:Stop()
	track2:Stop()
end)