How to find the number of children in a parent

So… I was working on a lag machine game that lags you out, my goal is to make a high score (the amount of unanchored parts), but there is also a button making it less laggy, so I will need to decrease the score by the amount of parts in the folder - and I am not sure how to find the number of children in an ancestor.
Any help appreciated!

You can use a function called :GetChildren()

For example you can do:

local amountOfChildren = folder:GetChildren )

Edit: nvm i reread your question.

:GetChildren() (returning an array of instances existing within a single level)
:GetDescendants() (returning an array of instances at every level below where it is called from)

Because it is an array, you can read the number of indexes it has. Your code would look like this:

local ChildCount = #Instance:GetChildren()

You call the method on the object of which you want to find the number of children.
For example if you want to know the number of children in a part you can do this-

local part = workspace.Part -- path to object
local Children_n = #(part:GetChildren()) -- # syntax returns the number of components in an array.

print(Children_n) -- prints the number of children

Helpful links-
[Instance:GetChildren() ](Instance:GetChildren (roblox.com))
[What does # mean in Lua? - Stack Overflow]

5 Likes