Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to count children? And make them a variable?

Asked by 6 years ago

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

Log in to vote
2
Answered by 6 years ago

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.

1local Players = game.Players:GetChildren() -- Returns a table
2 
3if #Players >= 4 then
4-- execute code
5end
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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

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

Let's break this down.

1local 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.

1local 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.

1local 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:

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

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

1local 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

1local playercount = #game:GetService('Players'):GetChildren()
2 
3if playercount > 5 then
4    print("There are more than five players on the server!")
5end

This can be simplified further to

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

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

Log in to vote
-1
Answered by
Oficcer_F 207 Moderation Voter
6 years ago

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

01local players = game.Players:GetPlayers()
02numberOfPlayers = 0
03 
04 
05for i,v in pairs(players) do
06 
07numberOfPlayers = numberOfPlayers + i
08 
09 
10end
11 
12 
13if numberOfPlayers >= 4 then
14 
15--execute code
16 
17end
0
This is an incredibly inefficient way of doing things plasmascreen 143 — 6y
0
regardless all examples provided work. TreySoWavvy 18 — 6y
0
it might be inefficient but hey either way it works so it can't be that bad. mudathir2007 157 — 6y
0
plus with this you can print the number of players in game mudathir2007 157 — 6y

Answer this question