Player.Character doubt

Hey!! Brief question

Its not possible to do this:

local isCharacter = Player:FindFirstChild("Character")
if isCharacter then
-- do stuff
end

By doing this, it will throw an error if the character is not accesable (like idk, the player is dead):

local isCharacter = Player.Character
if isCharacter then
-- do stuff
end

What would be a good approach to check if theres a Character in order to access its parts?

Thank you for your time and replies :3

Actually your second code is perfectly viable. Character is a property not a child, so trying to access it will not throw an error if it does not exist. It will simply be nil.

1 Like

Character is not a child of the player and thus it will always return nil on calling the function FindFirstChild on a player object.
To wait for the character to be loaded you can do this-
local character = Player.Character or Player.CharacterAdded:Wait()
if player.Character is nil it waits for the character to be added and thus yielding.

1 Like

Oh really?? :v
I thought it would throw an error, but yeah, that makes sense cause its not a child :woman_facepalming:

Thank you so much for your help @JarodOfOrbiter and @Zohair_028 !!

1 Like

Honestly what doesn’t make sense to me is that Roblox would have nil children cause an error. I’m not sure how it works behind the scenes but I know they could have done it so that it behaves like find first child instead. I’m sure I’m not smart enough to know the reason though.

1 Like

Yup, they should have their reasons why making it work like that. It would be helpful for us to not worry about getting errors and not be mandatory to do lots of pre-checks using FindFirstChild() and stuff like that. But ¯\_(ツ)_/¯ now Im used to use lots of checks before doing stuff u ,u