How to use WaitForChild properly?

Hello everyone, I’m trying to make a Framework with every accessible information. There’s some things that I’m not sure if them work as intended, one of them is WaitForChild.

I wanna ask you all: Using WaitForChild in a folder ensures that all their descendants loaded?

As an example of what I said: game:GetService("ReplicatedStorage"):WaitForChild("Framework").Services.Etc.Etc.Etc.Etc.Etc

A random successful test too:
image

1 Like

WaitForChild Simply waits for an Object to Exist, if it doesn’t exist, it will continue to wait until the item is found.


No, Using WaitForChild doesnt ensure all descendants are loaded, it simply waits until its loaded or found

6 Likes

How are you sure too? Well, I’m not saying you’re wrong, but, no one proved me wrong with any proofs… Some falsificationism in this post would be good.

If you don’t need to object to exist, use FindFirstChild Instead. WaitForChild Yields the script until the time limit is reached or the object is found.

3 Likes

The documentations (and many other posts) clearly mention that it waits on a specific child. You must include your own method of making sure all children of that instance are loaded. There are several forms of loading methods available to the public like PreloadService2 or you can use something like ContentProvider. Also you can set the wait time of :WaitForChild(“Name”,WaitTime) and form a recursive loop.

2 Likes

Yep, that detail i forgot, You can add a custom TimeOut by doing this:

P = script.Parent:WaitForChild("Part", 3) -- Waits for 3 seconds before timeout (This is optional tho according to description)

Documentation:
https://create.roblox.com/docs/reference/engine/classes/Instance#WaitForChild

4 Likes

I’m just going to make my framework until I come up with an error, probably I’ll get this solved.

1 Like

Read this:

4 Likes

It is not guaranteed that the descendants of a folder will be loaded immediately even if using WaitForChild() on it so, to stay away of any possible error that might happen, WaitForChild() is the best option for every descendant.
If you’re worried about readability using multiple WaitForChild() you can use this module instead to get a specific instance using a string path:

Edit: found this useful:

2 Likes

On the server, no need to use WaitForChild for objects you’ve already put into the explorer in studio.

local Model = workspace.Model
local Part = Model.Part1

On the client, use WaitForChild on the object-parent you would like to achieve, it’s descendants should be available by then. But I rarely need to wait for really specific parts on the client, if so use WaitForChild for all.

local Model = workspace:WaitForChild("Model")
local Part = Model.Part1

UI Objects descendants will always be present, if you use WaitForChild on the first parent frame for example.

local Gui = script.Parent
local ScoreboardFrame = Gui:WaitForChild("ScoreboardFrame")
local ScoreTextFrame = ScoreboardFrame.Frame.Left.TextFrame

If you’re uncertain if something is present, and only want the script to run if it is then do this:

local ShouldBeThere = workspace:FindFirstChild("Model2")

if not ShouldBeThere then return end -- Returns, most commonly used in functions

if not ShouldBeThere then continue end -- Continues, most commonly used in a for-loop. It continues to the next "thing" in the loop.

if ShouldBeThere then -- most commonly used, if you want to do a specific thing if ShouldBeThere is present, and still run the code below "that"
-- 
end
-- this is below "that"
1 Like

Thank everyone, I’ve finished my framework without errors, but I’ll always use WaitForChild.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.