WaitForDescendants()

https://www.roblox.com/library/11149732173/WaitForDescendants

A helper function for LocalScripts which lets you conveniently “WaitForChild” an instance and all of its descendants. Necessarily will lead to larger delays, but in most applications that is insignificant and will save you a lot of typing.

This script belongs in ServerScriptService.

To use WaitForDescendants, add the following to a LocalScript:
WaitForDescendants = require(game:GetService(“ReplicatedStorage”):WaitForChild(“WaitForDescendants”))

Calls in the local script look like this:
WaitForDescendants(instance) or WaitForDescendants(parent, childName)

Both will ensure the instance or parent.childName and its descendants that are visible to the server are loaded on the client.

If I had made this sooner, it probably would’ve saved me hours of typing while making Banana Island.

1 Like

I’m pretty sure :WaitForChild() already has this functionality built into it. I’ve never gotten an error when doing something like this, even on local scripts.

local modules = game:GetService("ReplicatedStorage").Modules;
local ExampleModule = require(modules.Example);

This is probably luck, for server scripts, WaitForChild isn’t really that useful since everything is basically guaranteed to be loaded, but on localscripts there is no guarantee the object exists when you query it so you should always be using WaitForChild

Regardless, you should only be using :WaitForChild() once in most cases.

:WaitForChild(instance) yields until instance and its descendants have loaded by default. So my example would still stand.

local modules = game:GetService("ReplicatedStorage"):WaitForChild("Modules");
local ExampleModule = require(modules.Example);
local ExampleModule2 = require(modules.Example2);
local ExampleModule3 = require(modules.Example3);

The only time I use :WaitForChild() more than once per parent is if the script itself is parented to it. There’s times where the script can load before the other children have, and it has caused me errors sometimes. Otherwise, you often don’t need to use :WaitForChild().

Though not fully relevent, I recommend reading this post if anyone is unfamiliar with using :FindFirstChild() or :WaitForChild().

1 Like

I also recommend reading that post:

“WaitForChild does not have any equivalents and it does not support recursion. It only supports waiting for a child by a given name, the first argument, a string.”

From the :WaitForChild() documentation:
waiting

I’ve done a few tests, and I’ve only had to use :WaitForChild() once per object. In my testing, :WaitForChild() yields until all descendants have loaded in.

Here is a test which shows :WaitForChild() does not yield until all descendants have loaded in.

Interesting. Well, for objects made during runtime, I would absolutely agree that you should use :WaitForChild() if you’re going to replicate and de-replicate objects in your game. I misunderstood the purpose of this module, my apologies.