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
character.ChildAdded:wait()
end
end
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.
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.
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
break
end
task.wait()
end
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.
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")
local Player = game.Player.LocalPlayer
local Character = Player.Character or Player.XharacterAdded:(Wait)
local Humanoid = Character:WaitForChild("Humanoid")
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!
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()
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