If you’d like a function to wait for many children, I made this and feel free to use it
function SharedUtils.WaitFor(par, list, timeout)
local items = {}
local start = tick()
local function doList(par, list)
for i,name in pairs(list) do
if type(name) == "table" then
local item = par:WaitForChild(i, math.max(.01, timeout - (tick() - start)))
if not item then return nil end
items[i] = item
doList(item, name)
elseif type(i) == "string" and type(name) == "string" then -- Rename
local item = par:WaitForChild(i, math.max(.01, timeout - (tick() - start)))
if not item then return nil end
items[name] = item
else
local item = par:WaitForChild(name, math.max(.01, timeout - (tick() - start)))
if not item then return nil end
items[name] = item
end
end
return items
end
return doList(par, list)
end
Example use:
local CharacterMembers = {
"Humanoid",
Head = {
"face"
},
"HumanoidRootPart",
"UpperTorso",
LowerTorso={"Root"},
"LeftUpperLeg",
"RightUpperLeg",
"LeftLowerLeg",
"RightLowerLeg",
"LeftUpperArm","LeftLowerArm","RightUpperArm","RightLowerArm","LeftHand","RightHand","LeftFoot","RightFoot"
}
local CharacterChildren = SharedUtils.WaitFor(character,CharacterMembers,5)
local face = CharacterChildren.face -- works
local leftlowerleg = CharacterChildren.LeftLowerLeg -- also works
However, keep in mind that you don’t need to use WaitForChild if:
-
A: You’re simply accessing something from ReplicatedStorage which exists at the start of the game(isn’t instanced by the server) - you only need to wait for things that are created by the server after the game starts, or things in the workspace
-
B: You can access client sided things by cloning them from ReplicatedStorage instead
What I mean by point B is, for the player gui for instance, I usually have a custom “StarterGui” folder in ReplicatedStorage. The client just clones assets from there into their playergui directly, so they don’t have to wait on anything being instanced from the server