Is there a reason this code doesn't work?

This is a localscript under StarterPlayer > StarterPlayerScripts.

local plr = game.Players.LocalPlayer.Character
game.Players.LocalPlayer.CharacterAdded:Wait()
local hum = plr.Humanoid
print(hum)

Also:

local plr 
game.Players.LocalPlayer.CharacterAdded:Connect(function(char)
if char ~= nil then plr = char end
end)
local hum = plr.Humanoid
print(hum)

Attempted to index nil with humanoid came up as an error in both code blocks. Really confused here, other solutions in posts aren’t working. Thank you for your time in advance.

1 Like

Script 1: The humanoid isn’t part of the player object, its part of their character:

local hum = plr.Character:WaitForChild("Humanoid")

Heres a script that works:

local player = game.Players.LocalPlayer
local character = player.Character:WaitForChild("Humanoid")
1 Like

plr is just a variable name, not the player object. If you look closely at the code, you’ll see that plr is listing the character, not the localplayer.

I see what you are trying to do here, the issue is you aren’t quite doing it right. You need to have an or statement when assining the character to a variable so that it will work.

If the first statement is false (nil is assumed false) then the other statement will be triggered.

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded
1 Like

You didn’t set the variable plr to anything so

This would actually error since you are indexing Humanoid to plr which contains nothing.

Try replacing this:

with this:

local plr 
game.Players.LocalPlayer.CharacterAdded:Connect(function(char)
if char ~= nil then plr = char end
end)
local hum = char.Humanoid
print(hum)
1 Like

It’s fine, would be hard to access that variable if it was defined inside the function…

In both instances of code plr is referencing LocalPlayer.Character. I understand it’s against usual naming conventions, but functionally still works. The issue is due to the code running before the Character is actually created and code snippet 1 by @dizzyscobbyScripting was almost right but not quite there.

1 Like

oh, I’m sorry. I read the code wrong lol.

it’s supposed to be something like this

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char.Humanoid
print(hum)

I’m sorry i just woke up lmao

1 Like

If i label the character with the or statement plr.Character or plr.CharacterAdded there are no answers for plr.Character and plr.CharacterAdded is just labeled as an RBXScriptSignal. What exactly am i supposed to do with this?

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

Just use this block of code for all future work.

1 Like

when you set the char variable to plr.Character or plr.CharacterAdded:Wait() it sets the variable to plr.Character first and if it fails because the character is not loaded yet, it would use plr.CharacterAdded:Wait() as a backup and yield it until the character is loaded.

1 Like

Thank you. Had me confused and quite angry alot last night, glad i don’t have to use things like repeat wait() until Player.Character or any type of loop.

1 Like