Attempt to index nil with character

I always get an error when I index a character, but a previous script I made before has never ever given me this error, and when I copy that code and paste it it still gives me the error.

local LocalPlayer = game.Players.LocalPlayer
local Character = LocalPlayer.Character
local Humanoid = Character.Humanoid

I have made the script a normal script, and tried turning it into a LocalScript.

2 Likes

The LocalPlayer property is only available on the client, your code must be under a LocalScript. Is your script a LocalScript and still giving this error?

2 Likes

If your localscript is still giving the error, it’s likely because your character hasn’t loaded yet.
Try adding this at the very beginning of the script:

repeat
wait()
until game.Players.LocalPlayer.Character ~= nil
wait(0.1) --this line optional, but I always put it

Always do use a wait when defining variables at the top of the script because the child hasn’t loaded yet. You won’t have to do that inside functions or events because the child should already loaded before you get to use them.

local LocalPlayer = game.Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.