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

What's the difference between WaitForChild and FindFirstChild?

Asked by
yoshi8080 445 Moderation Voter
8 years ago

I'm a bit confused on each each of those means and what they're used for. Can anyone explain the differences of these two?

1 answer

Log in to vote
4
Answered by 8 years ago

FindFirstChild returns the child if it can find a child of the instance you call it on with a name equal to the string you provide.

local child = instance:FindFirstChild("ChildName")

If it can't find the child it returns nil

You can use instance.ChildName but this will error if ChildName is not a valid member of instance. To get around the error you use FindFirstChild with a if statement

local child = instance:FindFirstChild("ChildName")
if child then --ensures child is not nil
    print'child exists!'
end

WaitForChild yields until it can find a child of the instance you called it on with a name equal to the string you provide

local child = instance:WaitForChild("ChildName")
print'child now exists!'

This is used if you want to yield code until a child exists

0
Wow, thanks for the help! I'll be able to use them a bit more, Thanks! yoshi8080 445 — 8y
0
No problem :D YellowoTide 1992 — 8y
0
So Basics its in the name. UserOnly20Characters 890 — 8y
Ad

Answer this question