How would I make it so some code could count the number of children in a model. The number of children would regularly be increasing and decreasing so you cant use a for loop with an increasing variable. Any help?
Number of children?
4 answers
The :GetChildren() method will return a list of each child in the target object so if I ran game.Workspace:GetChildren() in an empty baseplate template it would return each object in a list with things such as Terrain, the baseplate and anything else located inside of Workspace. By putting # before it it represents number of so by using #game.Workspace:GetChildren() you're effectively saying 'number of children in game.Workspace'. If it randomly changes the number of children you can readjust the variable accordingly as a new child gets added/removed (1), you could also have a while loop repeating forever (2) or just reassign the value with the correct number of children at the time you actually want to use it (3). Here's the code for each of the methods explained above:
Method 1 (Fairly efficient)
model = game.Workspace.Model -- The target model to search in
children = #model:GetChildren() -- Number of children in model (int)
model.ChildAdded:connect(function() -- When a new instance is added to model
children = #model:GetChildren() -- Update it to the number of children in model (int)
end)
model.ChildRemoved:connect(function() -- When a new instance is removed from model
children = #model:GetChildren() -- Update it to the number of children in model (int)
end)
Method 2 (Least efficient)
model = game.Workspace.Model -- The target model to search in
children = #model:GetChildren() -- Number of children in model (int)
while wait() do -- Repeat forever with a slight pause to prevent it freezing
children = #model:GetChildren() -- Update it to the number of children in model (int)
end
Method 3 (Most efficient)
function printChildren(target) -- This function could do anything, it doesn't just have to print the number of children
print(#target:GetChildren()) -- Prints number of children in target
end
wait(5)
printChildren(game.Workspace.Model) -- Print the children in Model
wait(5)
printChildren(game.Workspace.Model2) -- Print the children in Model2
Hope this helps and expands your knowledge, if it did in any way please don't forget to vote it up and accept it as the answer so people know you've had a sufficient answer.
you use #x, x being a table of items (in this case, the children of a model) in order to find the number. #(model:getChildren()) will return the number of children inside of model
model = workspace.ModelName -- replace with the name of your model here
number = #(model:getChildren())
print(model.Name .. " has " .. number .. " children!") -- this will print the # of children in output
1 comment
Just FYI, the parentheses in this case are redundant: #model:GetChildren() also works, as functions have higher priority than operators.
Give credit to Module_Scripter for answering this, but the best way to track the number of descendants of a model would be to use a variation of Module_Scripter's Method 1.
-- @author Module_Scripter
-- @editor Narrev
-- Config
local Model = game.Workspace.Model
-- Declarations
local Descendants = #Model:GetChildren()
-- Update functions
do
local function Add()
Descendants = Descendants + 1
end
local function Subtract()
Descendants = Descendants - 1
end
-- Connections
Model.ChildAdded:connect(Add)
Model.ChildRemoved:connect(Subtract)
end
You can try multiple things...
-- Function method
function HowManyChildren(Search)
print(#Search:children(), "- " .. Search.Name)
end
HowManyChildren(game.Lighting)
-- For method
for i = 1,#game.Workspace:children() do
print(i)
end
-- Pairs method
for i,v in pairs(game.Workspace:children()) do
print(i,v)
end
Hope this helped you!
-BloxSimple
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
There is no need to use an increasing variable in a for loop.