Variable humanoid error

I have a load variable for an animation:

local animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)

but I get this error when I test the game:
13:39:06.205 - Players.ffancyaxax12.Backpack.Pickaxe.Client:6: attempt to index nil with ‘Humanoid’

I copied the same script from another tool and it works fine, except this script does not.

You need to wait for the character to load before getting it’s humanoid.

repeat wait() until game.Players.LocalPlayer.Character ~= nil

Just put that before that statement, and it should work.

2 Likes

I think you can also do:

local animation = game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):LoadAnimation(script.Parent.Animation)
1 Like

Character was nil when the script was called.
@IntelligentCappy

local character = game.Players.LocalPlayer.CharacterAdded:Wait()
2 Likes

It says that the character is nil, so this would not work.

1 Like

Your player doesn’t have a character when you’re trying to call this. So to fix it (Since you said you took the script from a tool.) You can do the following code to wait for the character. Or you can put the local script you’re currently using in StarterPlayer > StarterCharacterScripts.

local character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local animation = character.Humanoid:LoadAnimation(script.Parent.Animation)
2 Likes