Attempt to index nil with 'Humanoid' weird bug

I am working on a mechanic in my game where when the mouse is moved, the camera moves along with it. But I am facing a very weird bug that I cannot figure out. When I run this code:

local m = game.Players.LocalPlayer:GetMouse()
local c = game.Players.LocalPlayer.Character
local h = c.Humanoid
local p = {m.X, m.Y}

it outputs with this error:

[Players.Grayseon.PlayerScripts.LocalScript:3: attempt to index nil with ‘Humanoid’

I have already tried to change the wording to it, even simply making the script use FindFirstChild in the workspace to find ‘Grayseon’ but it still has that error.

Also, this is a LocalScript in StarterPlayerScripts.

Thanks for helping me.

what’s the type of script local or normal server sided?

It is a LocalScript in StarterPlayerScripts.

did you try using waitforchild

Yes. But it says attempt to index nil.

this means game.Players.LocalPlayer.Character is nil.
c indexed with humanoid.
change c to

local c = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

the localscript runs quickly, so while you may see a character, when it runs, it doesn’t find it so localplayer.Character is nil.

1 Like
local m = game.Players.LocalPlayer:GetMouse()
local c = game.Players.LocalPlayer.Character
local h = c:WaitForChild("Humanoid")
local p = {m.X, m.Y}

The script runs before the Character And humanoid has loaded so that’s why it returns nil

1 Like

Ah, thank you. I was having a hard time figuring it out.

1 Like