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 not stop when a player unequipped a tool?

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

✓ Accepted answer

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()

1 comment

0

This works, but why does my idle animation not work when I re-equip the tool?

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