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

What is the benefit of FindFirstChild?

Asked by 6 years ago

What is the benefit of:

Instance:FindFirstChild("childName")

vs

Instance.childName

?

I assume that if childName is not a child, the first will return nil while the other will throw an error?

1
It is used so that if the instance child does not exist it will make the value nil instead of giving and error and stopping the entire scripts. It's mainly used to check if the child is there or not. LegitmateTrades 159 — 6y

1 answer

Log in to vote
2
Answered by 6 years ago
Edited 6 years ago

Instance.childname will cause a fatal script error if the child does not exist.

Instance:FindFirstChild("childname") will only return nil rather than breaking. Here's an example of the use:

if workspace:FindFirstChild("TestPart") then
    print("part exists!")
else
    print("part does not exist...")
end

If you merely used the former, this script would not work.

1
It does not return false, it returns nil User#19524 175 — 6y
0
Apologies, forgot to fact check ChemicalHex 979 — 6y
Ad

Answer this question