API:Class/Instance/FindFirstChild

FindFirstChild

Function of Object Icon.pngInstance
Ref<Instance> FindFirstChild (
    string name,
    bool recursive = false
)
Parameters:
  1. name
  2. recursive
    • Type: bool
    • Defaults to: false

Returns: Ref<Instance>
Description: Returns the first child found with the given name, or nil if no such child exists. If the optional recursive argument is true, recursively descends the hierarchy while searching rather than only searching the immediate object.

Notes

Here's a pure Lua implementation of FindFirstChild to demonstrate how it works:

local function findFirstChild(instance, name, recursive)
    for _, child in pairs(instance:GetChildren()) do
        if child.Name == name then
            return child
        elseif recursive then
            return findFirstChild(child, name, true)
        end
    end
end

Example

The below would look in Workspace for an object name "Brick". If found, it will change the name of the object to "Foo".

local found = workspace:FindFirstChild("Brick")
if found then 
  found.Name = "Foo"
end