How do I fix "attempted to index nil with ..."

I want to make a combat system but everytime i try to get the humanoid of the player i keep getting the error “attempted to index nil with …”

here is my current code

 local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character
local humanoid = character.WaitForChild("Humanoid")

i also tried

local character = script.Parent
local humanoid = character.humanoid
local player = game.PLayers.LocalPlayer

with no difference

What exactly is the issue?

Are you running the code in a Script or LocalScript?

LocalPlayer its only accessibile in LocalScripts

Edit:
Also WaitForChild() Is a method and you have to access It with :
char:WaitForChild("Humanoid")

Humanoid and character may not immediately load. So do:

local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

There are already multiple devforum posts on getting character, so please do check.
More info/source:

You could also check out: Player | Roblox Creator Documentation

1 Like

I’m running it from a local script inside starterplayerscripts

edit: I have also tried it with “findfirstchild” and by going to the player in players but the same error occurs

Character:waitforchild(“Humanoid”) returns the same error as the other attempts