Player.Character returns 'nil' in LocalScript

Trying to make a cool little death screen for fun and whenever I try to reference the character, it returns a nil value.

I’m not sure why Player.Character keeps returning nil, and I’ve never had this issue before. Is it deprecated or something?

Any ideas on what could be wrong?
image
image

Player.Character would be nil because it no longer exists, hence it being nil. This happens if the player has not spawned yet/had died. I recommend listening to the Player.CharacterRemoving and the Player.CharacterAdded function to listen for these changes.

2 Likes

Player.Character can be nil if you use it early in the script

It becomes nil because the character is not loaded yet while the script is running

Not sure if this would work since I can’t rlly test it bc im on mobile but

local character

game.Players.PlayerAdded:Connect(function(plr)
     plr.CharacterAdded:Connect(function(char)
            character = char
     end)
end)

Figured out what was wrong. Apparently even a LocalScript will run before the character is spawned in. (I thought this only applied to server-sided Scripts.)

Just had to wait out the spawning with repeat wait() until Player.Character and it was good.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.