Use of FindFirstChild()?

Help, I’ve been scripting for so long and I’m still confused on to what FindFirstChild() may do and where do I use it?

This is an embarrassing question tbh

:FindFirstChild() returns a instance inside a parent with the given name, otherwise it returns nil.

1 Like

:FindFirstChild() find the objects inside of something, for example:
local gui = game:GetService("StarterGui"):FindFirstChild("GUI")
It detects something inside of a ‘folder’ and returns an instance inside of the parent, otherwise turning it into a nil value.

@RealExoctic explained this simply and got straight to the point so well done!

2 Likes

So basically if you’re not sure whether some instance exists or not, you use :FindFirstChild(). In case the instance has been found, this’ll return the instance otherwise it’ll return nil.

If object hasn’t been found and you try to index it later then, you’d need a if statement that makes sure the instance actually exists; like shown in the sample below

local object = workspace:FindFirstChild("Object")

if object then --if there's a instance called "Object" in workspace, this gets passed
    print("Object has been found!")
end
6 Likes

FindFirstChild() is commonly used to know whether something is a child or not, since it can either return the child, or nil.

2 Likes

You mean if say you have a child of an instance with the same name as a property of said instance?

1 Like
if object:FindFirstChild("Child") then
    --Child is there
end

I can’t understand what you mean, but here is how it is used. It checks if the child isn’t nil.

My bad, I just reread it - sorry for the confusion!

As always @rokec123 goes into detail to provide as much information as possible - a wonderful teacher!

1 Like