Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What is the benefit of FindFirstChild?

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 comment

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.

1 answer

✓ Accepted answer

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.

2 comments

1

It does not return false, it returns nil

0

Apologies, forgot to fact check

This discussion is preserved as it was. New answers, comments, and votes are closed.