Does waitforchild("model") wait until all children of the model are loaded?

There isn’t, and I didn’t mess with any GUI except for my own which does not affect any other GUI’s.

Hmm, weird, could you go into test and show me a screenshot of your explorer window (mainly the bottom part of explorer)

I restarted my client tests and it works now. Must of been some bug.

Okay, good, Good luck further on your game!

1 Like

Wait for child,Waits for child specific so it only waits until the model has loaded it isn’t sure that all child of Model are loaded or not for eg

game.Players.LocalPlayer:WaitForChild("leaderstats").Cash

Can also give error sometime because of Cash not loaded

THIS IS TOTALLY A EXCEPTIONAL CASE AND HAPPENS WHEN CLIENT RUNS BEFORE SERVER SCRIPT FINISHES THE CHANCES OF THIS ERROR HAPPENING IS RELATIVELY LOW

3 Likes

I’ve had my script error 1/3 times so far. Perhaps I’ll just add a wait on the server script for loading purposes.

Is there some sort of preloading mechanism that exists that can load all models in a game?

You need to wait on every line

game.Workspace:WaitForChild("Model"):WaitForChild("stuff")--and so on
1 Like

What if I have like 30 parts inside one model, is there a way to loop through it?

You could do
repeat wait() until #model:GetDescendants() == expecteddescendants

3 Likes

Till i know you need to wait only when game has started like in pre function,So if you are doing some late or after some time then there isn’t a need of wait for child you can also just add these lines at 1st in every script

repeat wait() until game:IsLoaded()
or
game.Loaded:Wait()

You can also use Index method:

So what you’re saying is that if I just wait a few seconds it’s guaranteed to load?

Well No i mean you should wait until all object are loaded in either way i would highly prefer @index_nil way though

I don’t believe this is correct, otherwise (for example) doing game:WaitForChild("Workspace") may yield until the entire workspace had loaded with all descendants, which is not the behaviour.

Roblox instances are streamed in from a top-down model - i.e. if you have a model with 20 parts, and 1 part has a Light inside of it, the model will load, then the 20 parts, then last the Light will load in. It would not be safe to do workspace:WaitForChild("Model").LightPart.Light.

Edit: Useful post to clarify this for you :slight_smile:

10 Likes

That isn’t true, if you wait for just the Model it will only wait for the existence of that Model to appear, it doesn’t care about its children.

1 Like

Can you please remove that answer as a solution? Because it isn’t true, WaitForChild("Model") does not wait for all the model’s children to be loaded.

1 Like

Handling misinformation one step at a time, no it does not.

1 Like

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:
image

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.

1 Like

no, it does not. I have a folder and i am using waitforchild to wait for it and it doesnt have its children or its descendants

this happens aswell when i use a model instead of a folder.

Please do not necropost, and also refer to this announcement thread: