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.

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

if #Players >= 4 then
-- execute code
end
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.

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!

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:

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