Anybody knows what the exact lua equivalent to findfirstchild is?

Me and a friend are arguing about whether or not findfirstchild is actually doing

function Instance:FindFirstChild(name)
  for _, instance in ipairs(self:GetChildren()) do
    if instance.Name == name then
      return instance
    end
  end
end

or

function Instance:FindFirstChild(name)
  local instance
  pcall(function()
    instance = self[name]
  end)
  return instance
end

Anybody knows the real equivalent? It indexes an instance and returns a child of that name, or nil, without erroring if the child isn’t found.

I don’t know, I think the first one
we can also return nil after for loop end

I did a quick google search and found this GitHub page.

function Instance:FindFirstChild(name, recursive)
	for _, child in pairs(self:GetChildren()) do
		if child.Name == name then
				return child
		end
		if recursive then
				local result = child:FindFirstChild(name, true)
				if result then
						return result
				end
		end
	end
	return nil
end
2 Likes