How to use :WaitForChild()

Ello there! I’ve got a pretty simple question to ask.

So say I need to get something from replicated storage like so:

-- Option 1
local Pet = Game:GetService("ReplicatedStorage"):WaitForChild("PetFolder"):WaitForChild("World1"):WaitForChild("Legandarys"):WaitForChild("Dragon")

This code is sickening and I was wondering if all those :WaitForChilds() are necessary or if just using one :WaitForChild() would wait for all further children.

Like this:

-- Option 2
local Pet = Game:GetService("ReplicatedStorage"):WaitForChild("PetFolder").World1.Legandarys.Dragon

So, whats works option 1, or 2?

2 Likes

Both options work, but you don’t need that many waitforchilds like shown in option 1.

2 Likes

Adding more waitforchilds after doing :Waitforchild() is necessary, option 2 is cleanear and works the same as option 1.

It’s recommended to use WaitForChild when indexing a child of an Instance, if you’re using a LocalScript. The reason as to why is because how fast an Instance loads depends on the device the player is using’s performance

Instances stored inside of ReplicatedStorage are an exception on the above, since they’re replicated as soon as the player joins the game, so using WaitForChild is optional in this case (I personally still use it to keep the LocalScript’s code consistent)

3 Likes

But does 1 WaitForchild wait for all further children?
I’m getting mixed responses, some people say that I need to use WaitForchild A LOT like in option 1
and some say that i just need to use it once

Nope, but one waitforchild is enough for serverside scripts, because other children load almost instantly.

No, WaitForChild will only wait for the first Instance whose name matches the one provided in the argument to load

@0101100_0 server-side scripts don’t normally require using WaitForChild, unless it’s waiting for an Instance to be created by a separate server script

Some server-side scripts can be loaded faster than instances, and it will cause errors if you dont have any WaitForChild()'s, but yes, WaitForChild is necessary at stable situations.

I doubt that’s true, server-sided scripts are only supposed to run after each Instance present at the game’s default state has finished to load. Of course in situations such as player characters, since they’re not present at the game’s default state, then waiting would be appropriate, but I’ve never personally encountered an issue with a server script failing to find an Instance that was created in Studio’s edit mode. I could see having issues if you prefer creating Instances while play-testing in Studio, though

1 Like

Real roblox servers are kinda unstable, so it’s can cause some issues behind the studio.

2 Likes

I won’t argue with that, since I happen to be actively experiencing issues with joining servers on the Player :sweat_smile:

I have one more question

-- this is a localscript that i'm using in my gui
local Display = script.Parent:WaitForChild("Display")

--Option 1:
local Frame = Display.Frame


--Option 2:
local Frame = Display:WaitForChild("Frame")

Do I need to use Option 1 or 2.
I am wondering if the WaitForChild in the Display line carrys on over to Option 1 (so I don’t need to use WaitForChild because I just referenced a WaitForChild)

or if it doesn’t carry over and I should use another WaitForChild (Like in option 2)

1 Like

Option 2 is prefered since the Frame may take longer to load than the Display

The Display is also the Frame’s parent, which means the Display is guaranteed to load before the Frame


@rocketman392 Virtual highfive :hand::grin:

1 Like

Thanks! That makes sense! Give me a virtual highfive! :raised_hand:

1 Like

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