Eat animation not working

I made an eating animation for my food tool but my code doesn’t work. I’ve tried numerous solutions but they all do not work.
This is my current code:

local EatAnimation = script.Parent.Animation
local tool = script.Parent
local anim = nil
tool.Activated:Connect(function()
	print("activated")
	local hum =	script.Parent.Parent.Humanoid
	local anim = hum:LoadAnimation(EatAnimation)
	anim:Play()
end)

tool.Unequipped:Connect(function()
	if anim then
		anim:Stop()
	end
end)

The activated text prints successfully.

Have you welded the rig properly?

The animation instance may not have loaded in so you should try

local EatAnimation = script.Parent:WaitForChild("Animation")

and when loading the animation, you should use

local anim = hum.Animator:LoadAnimation(EatAnimation)

You’re wanting to wait for the Humanoid’s Animator to load, not the animation. Unless the animation is being instanced into the game, it should already be an object. (albeit with 0 practical use other than to stop other animations until it does load)

Hi, Sunglocto!

Try this and let me know if it works for you! :grin:

local Animation = script.Parent:WaitForChild("Animation")
local Tool = script.Parent

local LoadedAnimation = game:GetService("Players").LocalPlayer.Character:WaitForChild("Humanoid"):LoadAnimation(Animation)

Tool.Activated:Connect(function()
	LoadedAnimation:Play()
end)

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