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
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)
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
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
-- 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)