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.
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
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.
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")
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.
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.