Recently, I have seen in a YouTube tutorial a part of the code that interested me.
repeat wait() until player.CharacterAdded:Wait()
What does CharacterAdded:Wait()
Recently, I have seen in a YouTube tutorial a part of the code that interested me.
repeat wait() until player.CharacterAdded:Wait()
What does CharacterAdded:Wait()
I have never seen something like that ever be used with a busy wait. But what it does is wait for the CharacterAdded event to fire (aka, when a player’s character is added to the game), and the return of it, the character loaded, causes the busy wait to end because the result was truthy
although I recommend doing it like this
local character = player.Character or player.CharacterAdded:Wait()
IF character exist then store that, or wait for the character to be added and the return of the event is stored in the variable
All events have a Wait method which will yield until the event is fired once and will return whatever was passed from the event.
Is that actually something that comes from a YouTube tutorial? That’s bad code, the repeat is unnecessary. It’d just wait ~0.03 seconds for one iteration and then yield until CharacterAdded fires. It wouldn’t run any subsequent iterations because that’d be truthy (unless it’s not, then that’s a bug).
See RBXScriptSignal:Wait(). Yields until the signal fires and returns the arguments supplied.
That’s rather unnecessary and I don’t think it would even work depending on what you’re doing.
CharacterAdded:Wait() is VERY important actually.
It basically wait the Player’s Character to be loaded.
If the script ran without the character being loaded, the output would say something similar to “Character is not a valid member of player”
You should use this everytime you’re getting a player character on script.
(post deleted by author)
CharacterAdded:Wait() Would be used like when it is being Fired, Make an wait() Depending on the number you selected.