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

what's the difference between FindFirstChild and WaitForChild?

Asked by 9 years ago

I'm curious, what are the difference between FindFirstChild and WaitForChild?

2
FindFirstChild find the object your looking for and if it can't find it will return nil and WaitForChild wait till the Object your looking for exist. UserOnly20Characters 890 — 9y
0
The different between the two is that: 'FindFirstChild' will check to see if the Child exists within the Parent, if not, then it will return a 'nil' value, while 'WaitForChild' will yield the code until a Child with the same (exact) Name as the Argument is existant within the Parent, and will not return 'nil'. TheeDeathCaster 2368 — 9y

1 answer

Log in to vote
6
Answered by
davness 376 Moderation Voter
9 years ago

Easy.

FindFirstChild gives out a string value ("value"), if the selected child exists, or nil if no such child exists.

Example

local something = script.Parent:FindFirstChild("ChildName")
print(something)
--if a child with the given name exists, it will print it's name. Else, it will print nil.

Before we continue to WaitForChild, I have to say a thing. FindFirstChild has a bool property. If you do not type it, it will be false by deflaut. IF true, it will search for every descendant.

local something = script.Parent:FindFirstChild("ChildName", true)
print(something)
--if a descendant with the given name exists, it will print it's name. Else, it will print nil.
--if the hierarchy is like this (script.Parent.ChildNameA.Dakiy.ChildName), and bool recursive is true, it will print the child name and not nil. If false, it will just print the child name if the hierarchy is like that (script.Parent.ChildName), else it will print nil

WaitForChild does the same, but, if no child detected, it will wait until a child with the given name appears. And it has not the bool recursive

local something = script.Parent:WaitForChild("ChildName")
print(something)
--Basicly, it will never print nil, because if no child with the correct name, the function will yield until a child with the given name appears. Then, it will print it's name.
5
There is one additional difference: WaitForChild will *not* trigger if something's Name property is changed. WaitForChild listens to the ChildAdded event, and only looks at the Child actually added. adark 5487 — 9y
1
Cool! BringUpon 25 — 9y
2
@adark: I believe that is changed/is going to be changed soon, reducing lots of headaches! BlueTaslem 18071 — 9y
Ad

Answer this question