Is :WaitForChild():WaitForChild() Bad Practice?

Hey! I’m just wondering if its bad practice to do :WaitForChild():WaitForChild() twice in a row. Is this a necessary thing to do? I think I read long time ago that you should only use :WaitForChild() once and everything under it should be safe, but I’m not a hundred percent sure! But I also think on another occasion my script errored once since I didn’t :WaitForChild() under the children of the first :WaitForChild().

Example:

local sBlock = Barricade:WaitForChild("MainPart"):WaitForChild("Block")

I wasn’t able to find to find anything related to this topic! So I hope you know if this is bad practice!

Thanks!

3 Likes

I’m just giving you an answer that I think is right, but I could be wrong.
I think that in some cases this could be a bad practice for example if it is not necessary to wait for the child (in which case it will do the equivalent of a FindFirstChild()). But in some cases I’m sure it can be used, for example to wait for a screenGui to be loaded :

local ScreenGuiLoad = plr:WaitForChild('PlayerGui'):WaitForChild('ScreenGui')

Normally, when the ScreenGui is loaded, all its children are loaded too.

What about if its a script under a part in workspace that has:

local Barricade = script.Parent
local sBlock = Barricade:WaitForChild("MainPart"):WaitForChild("Block")

Should you WaitForChild for each children since this is an Instance.new()? And lets say I will clone them from ReplicatedStorage to workspace!

Honestly your best bet here and ways that I get around this is using the or operator.

local screenGUI = startGui:FindFirstChild('thatGui', false) or startGui:WaitForChild('thatGui', 5)

This has always worked for me personally and you get the findfirst child along with the waitfor child at the same time. You can turn this into a function

people already addressed your question several times on the forum.

that will tell you if what you’re doing is good or bad

that just turns one operation into two, I don’t see any difference as WaitForChild will act nearly the same if the item is already loaded and there.

1 Like

If the parts are likely to reach the workspace after the script starts, then yes I don’t see any problem in my opinion.

This work as well obviously , it may be a better way to code but you will not gain any performance optimization

This will also vary depending on everyones coding style and practical usage. No two developers are going to do everything exactly the same. I personally ensure everything is loaded and normally :FindFirstChild() is always the first thing that runs. :WaitForChild() is just my secondary backup in the case it isn’t loaded.

Also hint hint, using the built in timeout function is another step to this

1 Like

I remember reading this and I didnt see anything related to chaining :WaitForChild():WaitForChild(). I dont know if Im blind or something but I dont think this was addressed in the article.

But by the way. When you get script.Parent from a part, do you :WaitForChild for all the children of that part if the script is parented to the part?

-- Option 1
local Barricade = script.Parent
local sBlock = Barricade:WaitForChild("MainPart"):WaitForChild("Block")

or do you do this?

-- Option 2
local Barricade = script.Parent
local sBlock = Barricade:WaitForChild("MainPart").Block

or this but I think this errors:

-- Option 3
local Barricade = script.Parent
local sBlock = Barricade.MainPart.Block

If the MainPart will likely reach the workspace after the scripts run and the ‘Block’ Part is already inside the MainPart then the option 2 is your option

1 Like

thanks man! alright Ima fix all my scripts related to Instance.new and chaining :WaitForChild()

By the way, I suggest you find a better organization than putting your server scripts in workspace parts, you will end up with 60 scripts inside 60 differents parts, but it’s up to each individual. Maybe it suits you like this

this barricade is gonna be cloned from replicated storage so Im only gonna put serverscripts in parts that Im cloning which isnt too much!

1 Like

Hold on How come children of parented objects take longer to load in? childrens can take longer to load, for security i would keep the both waitforchild

ah so Option 1. That was real important to know! I’ve been doing this so I’m pretty safe which is nice!

local Barricade = script.Parent
local sBlock = Barricade:WaitForChild("MainPart"):WaitForChild("Block")

It kinda sucks doing :WaitForChild() like 3 times in a row sometimes but its necessary!

Thanks man!

1 Like

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