Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Attempt to index nil with Humanoid?

Asked by 3 years ago
Edited 3 years ago

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()
0
local ExclaburAnimationTrack = humanoid:LoadAnimation(ExcaliburAnimation) ExcaliburAnimationTrack:Play() Duck_Wuk 0 — 3y
0
That's what I did II_Goodlu 8 — 3y

2 answers

Log in to vote
3
Answered by
appxritixn 2235 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

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()
0
It says that ExcaliburAnimationTrack is an unknown global II_Goodlu 8 — 3y
2
Ah, a spelling mistake which I did not catch when I copied your code. Updated my answer. appxritixn 2235 — 3y
0
There are no errors but my animation isn't even playing :( II_Goodlu 8 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

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

Answer this question