Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Attempt to index nil with Humanoid?

So I had this error. If this question was already posted can you please tell me. Here is my code. I know I spelled something wrong. I updated my script based on the solution I was given but my animation isn't playing. Also want to thanks phxntxsmic, and JustinWe12 for trying to help me.

local ExcaliburAnimation = script["ExcaliburAnimation"]	
local player = game.Players.LocalPlayer
if not player.Character then
	player.CharacterAdded:Wait()
end
local char = player.Character
repeat
	char = player.Character
	wait()
until player.Character ~= nil
local humanoid = char.Humanoid

local ExcaliburAnimationTrack = humanoid:LoadAnimation(ExcaliburAnimation)

ExcaliburAnimationTrack:Play()

2 comments

0

local ExclaburAnimationTrack = humanoid:LoadAnimation(ExcaliburAnimation) ExcaliburAnimationTrack:Play()

0

That's what I did

2 answers

Your code is likely being called before the player's character is created. If the player's character has not yet been created, it will be equal to nil.

There are a few possible solutions to this issue. These are two solutions to the issue:

repeat
	wait()
until player.Character

and

if not player.Character then
	player.CharacterAdded:Wait()
end

Inserted into your code it would looks something like this:

local ExcaliburAnimation = script["Excalibur Animation"]
local player = game.Players.LocalPlayer
--if not player.Character then
--	player.CharacterAdded:Wait()
--end
-- ^^ that would work when un-commented
local char = player.Character
repeat
	char = player.Character
	wait()
until player.Character ~= nil
local humanoid = char.Humanoid

local ExcaliburAnimationTrack = humanoid:LoadAnimation(ExcaliburAnimation)

ExcaliburAnimationTrack:Play()

3 comments

0

It says that ExcaliburAnimationTrack is an unknown global

2

Ah, a spelling mistake which I did not catch when I copied your code. Updated my answer.

0

There are no errors but my animation isn't even playing :(

Sometimes the character will load after the script has ran. It says humanoid index nil so I assume that character is nil. You should add a script that waits for the character to load in.

if not char then
	char = player.CharacterAdded:Wait()
end

This should solve it if you insert it between 3 and 4

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