What exactly is this line of code doing?

I found some code on the devforum that I was gonna use and it works, but I don’t quite understand what this line of code is doing.

local Character = Player.Character or Player.CharacterAdded:Wait()

I get the Player.Character part but that usually would error because the player’s character isn’t loaded yet. Does or Player.CharacterAdded:Wait() somehow fix this?

In my view . It is because a script run very fast. when the script run . This line Player.Character will search for character in player and if it is not found it will give error . Player.CharacterAdded:Wait() will wait until character is added to player

basically it checks to see if the character exists using Player.Character
if there is a character then the variable is equal to that character

if there isn’t a character then Player.CharacterAdded:Wait() runs
it basically waits until the character is added, and then it returns the character
since it returns the character, the variable is equal to that character

1 Like

This is called an inline expression.
More information can be found here: conditional - Inline conditions in Lua (a == b ? "yes" : "no")? - Stack Overflow

So it’s sort of like :WaitForChild() but in continually runs untill the instance is found?

If the Player.Character is nil, Player.CharacterAdded:Wait() will yield until the character has been added and return it.

Read my reply for more information above.

you know the CharacterAdded connection?
well if you add :Wait() at the end it will wait for that function to run

WaitForChild is not the same as :Wait()

Sources:

https://developer.roblox.com/en-us/api-reference/event/Player/CharacterAdded

1 Like