[ERROR] Attempt to index nil with 'LoadAnimation'

Hello there, so I keep getting this (topic name) error and I’ve checked several other topics, adjusted my code and nothing happens. Anyways, here’s my code.

	if Debounce == false then
		UseTool()
		local NewAnimation = Humanoid:LoadAnimation(SlashAnimation)
		NewAnimation:Play()
		Debounce = true
	else
		wait(.1)
		Debounce = false
		UseTool()
		local NewAnimation = Humanoid:LoadAnimation(SlashAnimation)
		NewAnimation:Play()
		Debounce = true
	end
end)

Hello!
The error means the Humanoid is not being retrieved correctly, and is instead nil. Please share the code where Humanoid is retrieved and defined.

2 Likes

Here’s the code for Humanoid

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChild("Humanoid")

I’m assuming this is a localscript (if it is not, it will not be able to use .LocalPlayer from Players).

Try defining your Humanoid as:

local Humanoid = Character:WaitForChild("Humanoid");

and let me know how it goes.

Edit: This was a solution because scripts load faster than characters. The script then waited for the character to load, and attempted to find Humanoid. The character loads before the Humanoid, so the script found nil and saved that. Having :WaitForChild() forces the script to wait until the Humanoid loads.

4 Likes

There’s no errors but the Animation isn’t playing. My character is just standing there.

Progress!
Could you show me where SlashAnimation is defined? And ensure that AnimationId is set correctly.

Never mind actually I didn’t have an AnimationID set lol. Thanks for your help!