Filip_YTB
(FiGiUL)
October 15, 2022, 10:00am
#1
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")
QUlCKRED
(arii)
October 15, 2022, 10:06am
#3
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:
Once you have a reference to the Player object/Instance, Player.Character can be used to reference their Character Model.
As an extra precaution, you may want to define their Character as
local Character = player.Character or player.CharacterAdded:Wait()
in case there’s a circumstance where you may need to wait for the player’s Character to respawn/load before performing further actions in the script.
Edit (November 1st, 2021): Here are some examples that might be useful!
LocalScript Ex…
You could also check out: Player | Roblox Creator Documentation
1 Like
Filip_YTB
(FiGiUL)
October 15, 2022, 10:06am
#4
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
Filip_YTB
(FiGiUL)
October 15, 2022, 10:10am
#5
Character:waitforchild(“Humanoid”) returns the same error as the other attempts