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!
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.
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
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
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
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