Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How does the part of code:WaitForChild() mean? Why is it helpful?

Asked by 4 years ago

I hear very much in scripts you say :WaitForChild(). Why do you use it? Why is it useful? How does it work? I want to know. Thank you for helping!

Sincerely,

Ducksneedhelp

0
Wait for child basically means the script to wait for the player than play the script.Because the scripts play faster than your game even loads.This line is used in many scripts in games Abhin0123 -10 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

Explanation

It is extremely useful because whenever you start a game or join one, some instances may not have loaded yet and WaitForChild is really efficient. Let's suppose you have a large map in your game and it takes some time to load. You'll have to use WaitForChild so that it ensures that the map is FULLY loaded.

Here are some examples. The script down below is for my game that ensures that things UI's and maps are loaded in my game.

local map = game.ServerStorage:WaitForChild("Map1") -- Waits for it to load and that it IS in the ServerStorage

wait(5)

map:Clone() -- Clones the map 

wait(2) -- Small delay

map.Parent =  workspace -- Parent's it to the workspace

Things to know

WaitForChild is used when you want the script to wait for something to load before continuing the whole script.

WaitForChild is extremely important for variables and it is really efficient as well.

Let's say that If you store a map in ServerStorage or ReplicatedStorage, it may not have loaded when you start the gameWaitForChild is going to be used. An example is down below!

local fightingMap = game.ReplicatedStorage:WaitForChild("Map1') 

local sword = game.ReplicatedStorage:WaitForChild("Sword")

local parts = game.ServerStorage:WaitForChild("Parts")

Please make sure to select this as your answer if this helped!

Ad
Log in to vote
-1
Answered by
Lyphios 77
4 years ago
Edited 4 years ago

So WaitForChild() waits for an instance that doesn't yet exist or hasn't been created yet, like a player or character, or for something to fully load in before the script fires. You should just read the article on developer.roblox.com here: https://developer.roblox.com/en-us/api-reference/function/Instance/WaitForChild . It explains everything you need to know.

Answer this question