Attempt to index nil with 'LoadAnimation()'

Ello,

I’ve used the :LoadAnimation() function many times before, but today I’m not entirely sure why it’s not working. When I test out the script, I get the error below saying attempt to index nil with ‘LoadAnimation()’. If you can help me out I would very much appreciate it, btw, everything is inside a LocalScript.

4 Likes

Are you assigning humanoid to the right varible? I don’t think you should use or when assigning variables.

But did you :Play() it? I don’t see any instances of you playing it in this picture

No I think the problem is inside the script assinging the variable humanoid

That shouldn’t be an issue. I’ve turned the humanoid into a variable and it worked. Also, I can clearly see you post. You don’t want to clog up a topic with repeated posts.

1 Like

Always use :WaitForChild() when getting stuff inside the character. This is because sometimes, the character does not load in properly, therefor causing your script to completely break.

Instead of FindFirstChild(“Humanoid”) use WaitForChild() and see if anything changes.

9 Likes

Yeah that seems like the solution for assinging the humanoid value.

2 Likes

Thanks so much! This seemed to be the problem.

1 Like

Wow, very true, thank you for your Always use :WaitForChild() , it really helps me! :smiley: :smile:

3 Likes

This really helped me, thank you! :smile:

I have the same error but :Waitforchild() doesn’t work… does anyone have a solution?

local player = game.Players.LocalPlayer
local Char = player.Character
local Humanoid = Char:WaitForChild("Humanoid")

local AttackAnim = Humanoid:LoadAnimation(SECRET)
AttackAnim.Priority = Enum.AnimationPriority.Action
AttackAnim.Looped = false

local OneShot = Humanoid:LoadAnimation(SECRET)
OneShot.Priority = Enum.AnimationPriority.Action
OneShot.Looped = false


local Sword = script.Parent
local blade = Sword.Handle.Blade
local Cooldown = 2 -- in seconds
local db = true

Sword.Activated:Connect(function()

	if db then
		db = false
		AttackAnim:Play()
		blade.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChildWhichIsA("Humanoid") then
				if hit.Parent:GetAttribute("Enemy") then
					hit.Parent.Humanoid.Health -= 10
					print(hit.Parent.Humanoid)
				end
			end
		end)
	end
	wait(Cooldown)
	db = true
end)

Try this:

local player = game.Players.LocalPlayer

local Char = player.Character or player.CharacterAdded:Wait()

local Humanoid = Char:WaitForChild("Humanoid")

local AttackAnim = Humanoid:LoadAnimation(SECRET)
AttackAnim.Priority = Enum.AnimationPriority.Action
AttackAnim.Looped = false

local OneShot = Humanoid:LoadAnimation(SECRET)
OneShot.Priority = Enum.AnimationPriority.Action
OneShot.Looped = false

local Sword = script.Parent
local blade = Sword.Handle.Blade
local Cooldown = 2
local db = true

Sword.Activated:Connect(function()
	if db then
		db = false
		AttackAnim:Play()
	
		local conn
		conn = blade.Touched:Connect(function(hit)
			local targetHum = hit.Parent:FindFirstChildWhichIsA("Humanoid")
			if targetHum and hit.Parent:GetAttribute("Enemy") then
				targetHum.Health -= 10
				print(targetHum)
			end
		end)
	end
	wait(Cooldown)
	db = true
end)
1 Like