Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to count children? And make them a variable?

How can I count children and find out their number? For example: When the number of children is 4 (4 players on the server), the following command is executed.

3 answers

There probably are many ways to do this, but the way I would do this is to get a table of values, and then getting the amount of values within that table.

The code below gets the number of players connected to the game.

local Players = game.Players:GetChildren() -- Returns a table

if #Players >= 4 then
-- execute code
end

Lets say you want to get the amount of parts in a model named "Sports Car", and this model is located in the workspace.

local model = workspace:FindFirstChild("Sports Car")
local children = model:GetChildren()
local childcount = #children

Let's break this down.

local model = workspace:FindFirstChild("Sports Car")

Here we are finding the "Sports Car" model, and storing it in a variable. This isn't necessary here, as we are only using it once, and should only be done if you're re-using the model variable. I'm doing it for tutorial purposes.

local children = model:GetChildren()

The function :GetChildren() puts all the children of an instance into a table, and returns that table. Therefore, the variable 'children' contains every single child of "Sports Car". However, it does not contain any children of children - this gets more complicated.

local childcount = #children

This is simple - putting a hash in front of the name of a table variable, or function that returns a table, returns the amount of indexes in that table (i.e. the length of the table).

Thusly, this can be simplified to one line:

local childcount = #workspace["Sports Car"]:GetChildren()

To get the number of players in a server, therefore, you do this:

local playercount = #game:GetService('Players'):GetChildren()

At this point, we can easily add a conditional statement to execute some code if the player count is high enough

local playercount = #game:GetService('Players'):GetChildren()

if playercount > 5 then
	print("There are more than five players on the server!")
end

This can be simplified further to

if #game:GetService('Players'):GetChildren() > 5 then
	print("There are more than five players on the server!")
end

I hope this helps! If it answered your question, please select this as the correct answer so that others can benefit from it!

@TreySoWavvy's example is the best, but this is another alternative:

local players = game.Players:GetPlayers()
numberOfPlayers = 0 


for i,v in pairs(players) do

numberOfPlayers = numberOfPlayers + i 


end 


if numberOfPlayers >= 4 then

--execute code 

end

4 comments

0

This is an incredibly inefficient way of doing things

0

regardless all examples provided work.

0

it might be inefficient but hey either way it works so it can't be that bad.

0

plus with this you can print the number of players in game

This discussion is preserved as it was. New answers, comments, and votes are closed.