Questioin about "player.CharacterAdded:wait()"

I dont understand what “player.CharacterAdded:wait()” means here.

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
local humanoid
while not humanoid do
    humanoid = character:FindFirstChildOfClass("Humanoid")
    if not humanoid then
1 Like

player.CharacterAdded:Wait() simply waits for the player’s Character to exist since the script could be running before the character has loaded into the game.

1 Like

When this script runs, the player’s character might not be in the game yet.

local variable = x or y is shorthand for:
if variable != nil then variable = x else variable = y

So if the character isn’t loaded in the game yet, it will perform player.CharacterAdded:wait(), which basically waits until the character is loaded, then assigns it to the variable.

Edit: Accidentaly posted, sorry for that!

1 Like

Another question: If " humanoid = character:FindFirstChildOfClass(“Humanoid”)" is mean to find Humanoid in Character, why would “character.ChildAdded:wait()” be needed because Child might not even be Humanoid

This part of the code is a bit weird. You could just do

while true do
    local humanoid = character:FindFirstChildOfClass("Humanoid")
    if humanoid then

As for why the .ChildAdded is there, if it wasn’t then the loop would crash the game. Other than that, it’s making the loop wait until something has been added to character, because it would be inefficient to always check if there’s a humanoid even if nothing’s been added.

1 Like

It checks if the humanoid exists inside the character. If it doesn’t exist then it will wait until a child is added into the character. It will then do this process repeatedly until the humanoid finally exists

A better code would be

repeat wait() until character:FindFirstChildOfClass("Humanoid")

since this would only use 1 single line for waiting on the humanoid

The final script would be

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

repeat wait() until character:FindFirstChildOfClass("Humanoid")

local humanoid = character:FindFirstChildOfClass("Humanoid")
1 Like

Or you could just to this:

local Player = game.Player.LocalPlayer
local Character = Player.Character or Player.XharacterAdded:(Wait)
local Humanoid = Character:WaitForChild("Humanoid")

Make sure to mark a post as the Solution if the original post has been solved

First off, NEVER use a lowercase wait(). Instead, use Wait(); player.CharacterAdded:Wait() is a simple way of the script to detect when the character is added to workspace! It’s the same as the Character Added event, but a more compact way of executing it! :smiley:

Aren’t wait() and Wait() the same? I thought for a second that you would say that wait() was depecrated

They’re different

You shouldn’t even be using wait(n) (With a lowercase w) in the first place to begin with anyways, since I believe task.wait(n) is more efficient

Wait() (With a uppercase W) are used for Events, and will yield until that said Event is fired once

I know what they mean and that task.wait() is more efficient, im just asking whether event:wait() and event:Wait() are the same.

They are, it’s just that wait() is deprecated just like using connect() (It’s a minor inconvenience issue but should just be preferred) and should be replaced in favor with a capital instead Wait()/Connect()

I know what while true do is. But what is “while not humanoid do”? Also what is reason the loop is below “local humanoid”?

local humanoid is a variable to hold the humanoid that exists outside the scope of the loop. This way the loop doesn’t create the variable each time the loop runs. If you created the variable in the scope of the loop then every time the loop runs it would initialize the variable local humanoid to nil.

local humanoid` is actually the same as `local humanoid = nil

while not humanoid do is a loop. Of course, you’re familiar with while loops.

not inverts an expression.

So not humanoid is the same as humanoid == nil. Therefore if the variable humanoid is equal to anything other than false or nil then it’s true. For example, if you had the code below.

local humanoid = true

Then not humanoid would be humanoid == false or humanoid == nil

1 Like

It was awesome explanation for me to understand and thank you. Had me search how while loop works.

Thanks, and my bad, you mentioned you knew what while true do is. So I figured you knew what loops were already.

1 Like

Wait, if ‘humanoid == nil’ condition to be true, doesn’t humanoid variable have to be nil or false?

That’s correct, if you type a variable without assigning a value then it initializes the variable to nil by default.

local humanoid

There is a catch though. You cannot initialize any other variable under that variable.

local humanoid
local water = 100 -- this won't work. You need to assign a value to humanoid before you can define any variables after humanoid.

However this works

local water = 100
local humanoid

And of course, since humanoid initializes to nil by default, the following code resolves to true.

local humanoid

if not humanoid then print("No humanoid exist.") end
1 Like