Reviving the topic with more knowledge in my noggin.
TL;DR
When streaming is disabled, WaitForChild will wait until all descendants of an instance are there.
When streaming is enabled, WaitForChild wont wait for BaseParts descendants in your instance, refer to this to learn more.
If you have streaming disabled then you can expect to use WaitForChild only in rare occasions, the reason being is that all replicable instances would have replicated before LocalScripts inside StarterPlayer run.
Let’s verify this, before I start the server by hitting Play
, I have 627 instances under Workspace
:
Now i’ll put this code in StarterPlayerScripts
:
print(#workspace:GetDescendants())
When I hit play, the above code does in fact print 627, indicating that all instances under Workspace were ready before that script ran, which means no WaitForChild will be needed for instances that exist in replicable containers (Workspace, ReplicatedStorage, Lighting etc.) before the server starts running.
Now with that let’s answer if WaitForChild
waits for all children of an instance.
With streaming disabled let’s consider the following code:
--[[ SERVER ]] --
local model = Instance.new("Model")
model.Name = "Model"
local lastParent = model
for _ = 1, 100 do
lastParent = Instance.new("Part", lastParent)
end
print(#model:GetDescendants()) -- 100
model.Parent = workspace
--[[ CLIENT (StarterPlayerScripts) ]]--
print(#workspace:WaitForChild("Model"):GetDescendants()) -- 100
The server creates a Model
and nests 100 Parts
to it then parents it to Workspace:
In that case we have to use WaitForChild because we cant guarantee that Model
will be in Workspace
before the client’s script runs, and when we use it we can expect all those Parts to be in the model.
There are some things i glossed over but hopefully the main point has reached. If you have any questions feel free to ask.