How to use WaitforChild

Lets say I have this code:

local a = ServerStorage:WaitForChild("ParentTestA")
local b = a:WaitForChild("ChildTestB")

Do I have to use :WaitForChild to get ChildTestB even though I already got its parent using :WaitForChild()?

6 Likes

Content in ServerStorage are already loaded just like replicatedstorage those are all static content, so you don’t need WFC. As for your question once a parent has loaded all of its children load too.

6 Likes

Does that mean if I do game:WaitForChild("Workspace") all the contents of workspace will be loaded?

2 Likes

No not really, workspace is static but its content is not as they need to replicate.

2 Likes

Have you tried doing a WaitForChild() on the BoxOpeningGui?

4 Likes

As for your question once a parent has loaded all of its children load too.

I don’t think this works since in one of my local scripts I am doing this:

local player = Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")

The script is in starterplayerscripts, and it errors: BoxOpeningGui is not a valid member of PlayerGui "Players.dutycall11.PlayerGui" I have checked and BoxOpeningGui is present in PlayerGui

2 Likes

It’s not static, that’s why, it’s created upon join.

2 Likes

Have you tried doing a WaitForChild() on the BoxOpeningGui?

1 Like

When I use WaitForChild the code works and there is no error. However I am confused on what @weakroblox35 said which is that once a parent has loaded all of its children load too. If this was true, why was there an error when I used “.” but not an error when I used WaitForChild.

local player = Players.LocalPlayer local playerGui = player:WaitForChild("PlayerGui")
The script is in starterplayerscripts, and it errors: BoxOpeningGui is not a valid member of PlayerGui "Players.dutycall11.PlayerGui" I have checked and BoxOpeningGui is present in PlayerGui

1 Like

Sorry for bumping this but I still haven’t found a solution to this problem

Contents in PlayerGui isn’t loaded on player join, instead when the character spawns in, even guis that have ResetOnRespawn (or whatever its called) off.

Whenever you have server code running you don’t need to wait for the playergui, instead wait for guis inside to change

for example

local player = Players.LocalPlayer
local playerGui = player.PlayerGui --dont need waitforchild
local boxOpeningGui = playerGui:WaitForChild("BoxOpeningGui") --now we need waitforchild because this code is running before the player has spawned in

all WaitForChild actually does is check if the child is in the parent, and if not, wait for it.

I barely use WFC except for Gui. You used to need to use WFC on everything in the olden days of roblox before I started scripting. However now the static children of the instance run before any WaitForChild thing is executed. Which is why you don’t need to do

game:WaitForChild("Workpace"):WaitForChild("Map"):WaitForChild("Part")

I don’t know why no one just told you what waitforchild does. It basically waits till something is loaded in, so waitforchild is used for when you know certain it’s gonna be there but it has a chance to not have loaded in instantly when the game started

If you use waitforchild once, the descendants are also loaded.

Is this for a loading screen? Just do

if not game:IsLoaded() then
 game.Loaded:Wait()
--make your loading screen visible and stuff
end
if game:IsLoaded() and game.Loaded then
--do stuff that you'd do when it's loaded
end

PlayerGui is already loaded, it always is. The UI hasn’t been cloned thought the game tells it it’s ready to go due to the fact there’s no children since they haven’t been cloned. Do PlayerGui:WaitForChild(“BoxOpeningGui”) instead and it will work.

also btw you can just say game:GetService(“Workspace”); you dont need to use waitforchild for that

You don’t need any of that Jesus Christ people. Unless the script is in replicated first or the specific instance gets created/parented then do not wait.

local ss = game:GetService("ServerStorage")

local a = ss:WaitForChild("ParentTestA")
local b = a:WaitForChild("ChildTestB")

print(b)

Because it’s not loaded yet. Load up takes a moment.
local part = workspace:WaitForChild(“Map”):WaitForChild(“Part”)


local part = workspace:WaitForChild(“Map”):WaitForChild(“Parts”)
:WaitForChild(“RightSide”):WaitForChild(“Lower”)
:WaitForChild(“Green”):WaitForChild(“Part”)

print(part)


local part = workspace:WaitForChild(“Map”):WaitForChild(“Parts”)
:WaitForChild(“RightSide”).Lower.Green.Part – still may error, probably not

print(part)


task.wait(3) – I’m not adding all them WaitForChild()'s
local part = workspace.Map.Parts.RightSide.Lower.Green.Part

print(part)

the scripts execute the instant they are loaded in, so that may mean that some assets aren’t loaded in yet. waitforchild gives it a lil time