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
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
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!
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.
This discussion is preserved as it was. New answers, comments, and votes are closed.
3 comments
See https://developer.roblox.com/en-us/api-reference/function/Instance/WaitForChild
https://scriptinghelpers.org/questions/19379/difference-between-findfirstchild-and-waitforchild
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