Why isn't my script working?

Hello! This is my first post so please ignore the mistakes…

I am making a tool that plays an animation once you equip and stops when you unequip.

When you equip it works perfectly but when I unequip the animation still plays. There are no errors in the output either so I’m pretty much lost.

https://gyazo.com/75f04c6b3bfa7ae435dea227ea23b2bc

This is my script right now.

local Idle = script.Parent.Idle
local Eat = script.Parent.Eat
local Tool = script.Parent

script.Parent.RemoteEvent.OnServerEvent:Connect(function()
	local EatAnimTrack = Tool.Parent.Humanoid:LoadAnimation(Eat)
	EatAnimTrack:Play()
end)

script.Parent.Equipped:Connect(function()
	local IdleAnimTrack = Tool.Parent.Humanoid:LoadAnimation(Idle)
	IdleAnimTrack:Play()
end)
local Idle = script.Parent.Idle
local Eat = script.Parent.Eat
local Tool = script.Parent

script.Parent.RemoteEvent.OnServerEvent:Connect(function()
	local EatAnimTrack = Tool.Parent.Humanoid:LoadAnimation(Eat)
	EatAnimTrack:Play()
end)

script.Parent.Equipped:Connect(function()
	local IdleAnimTrack = Tool.Parent.Humanoid:LoadAnimation(Idle)
	IdleAnimTrack:Play()
end)

script.Parent.Unequipped:Connect(function()
	local IdleAnimTrack = Tool.Parent.Parent.Character.Humanoid:LoadAnimation(Idle)
	IdleAnimTrack:Stop()
end)

script.Parent.Unequipped:Connect(function()
	local IdleAnimTrack = Tool.Parent.Parent.Character.Humanoid:LoadAnimation(Idle)
	IdleAnimTrack:Stop()
end)

Any help will be greatly appreciated! Thank you!

1 Like

This does not work. You will have to make a table of all animations and stop them by doing a loop:

Code

local AnimationTracks = humanoid:GetPlayingAnimationTracks()

  • – Stop all playing animations
  1. for i, track in pairs (AnimationTracks) do
  2. track:Stop()
  3. end
3 Likes

So it should look like:

Code

local Idle = script.Parent.Idle
local Eat = script.Parent.Eat
local Tool = script.Parent

script.Parent.RemoteEvent.OnServerEvent:Connect(function()
local EatAnimTrack = Tool.Parent.Humanoid:LoadAnimation(Eat)
EatAnimTrack:Play()
end)

script.Parent.Equipped:Connect(function()
local IdleAnimTrack = Tool.Parent.Humanoid:LoadAnimation(Idle)
IdleAnimTrack:Play()
end)
local Idle = script.Parent.Idle
local Eat = script.Parent.Eat
local Tool = script.Parent

script.Parent.RemoteEvent.OnServerEvent:Connect(function()
local EatAnimTrack = Tool.Parent.Humanoid:LoadAnimation(Eat)
EatAnimTrack:Play()
end)

script.Parent.Equipped:Connect(function()
local IdleAnimTrack = Tool.Parent.Humanoid:LoadAnimation(Idle)
IdleAnimTrack:Play()
end)

script.Parent.Unequipped:Connect(function()
local AnimationTracks = humanoid:GetPlayingAnimationTracks()
2. for i, track in pairs (AnimationTracks) do
3. track:Stop()
4. end
end)

script.Parent.Unequipped:Connect(function()
local AnimationTracks = humanoid:GetPlayingAnimationTracks()
2. for i, track in pairs (AnimationTracks) do
3. track:Stop()
4. end
end)

2 Likes

The problem is when you unequip the code creates a NEW animation track and tries to stop it. However, it’s not the same one that is playing. So when you try to stop it, it tries to stop the new animation track that isn’t even playing. To reference the original animation track you will need to store it to reference to later. I made an example here:

local Idle = script.Parent.Idle
local Eat = script.Parent.Eat
local Tool = script.Parent

local EatAnimTrack
local IdleAnimTrack

Tool.RemoteEvent.OnServerEvent:Connect(function()
	if not EatAnimTrack then EatAnimTrack = Tool.Parent.Humanoid:LoadAnimation(Eat) end
	EatAnimTrack:Play()
end)

Tool.Equipped:Connect(function()
	if not IdleAnimTrack then IdleAnimTrack = Tool.Parent.Humanoid:LoadAnimation(Idle) end
	IdleAnimTrack:Play()
end)

Tool.Unequipped:Connect(function()
	if IdleAnimTrack then
		IdleAnimTrack:Stop()
	end
end)

Curious on how the original code had duplicate lines, but in my example, I fixed it up. Hope this helps!

Edit: Got rid of debug prints and fixed grammar.

2 Likes