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
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.
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.
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:
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"