Scripting / Method
FindFirstChildOfClass()
Finds the first direct child whose ClassName exactly matches the requested class.
Syntax
instance:FindFirstChildOfClass(className)Parameters and return value
| Name | Type | Description |
|---|---|---|
className | string | Exact ClassName to search for among direct children. |
return | Instance or nil | The first matching direct child, or nil when no match exists. |
This method searches direct children only. It does not have a recursive argument.
Example: find a Humanoid
Luau
script.Parent.Touched:Connect(function(hit)
local character = hit.Parent
local humanoid = character and character:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.Health = 0
end
end)FindFirstChildOfClass vs IsA
FindFirstChildOfClass() compares the direct child's concrete ClassName. For example, searching for "BasePart" does not match a Part merely because Part:IsA("BasePart") is true.