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.
1 | local model = workspace:FindFirstChild( "Sports Car" ) |
2 | local children = model:GetChildren() |
3 | local childcount = #children |
Let's break this down.
1 | 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.
1 | 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.
1 | 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:
1 | local childcount = #workspace [ "Sports Car" ] :GetChildren() |
To get the number of players in a server, therefore, you do this:
1 | 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
1 | local playercount = #game:GetService( 'Players' ):GetChildren() |
4 | print ( "There are more than five players on the server!" ) |
This can be simplified further to
1 | if #game:GetService( 'Players' ):GetChildren() > 5 then |
2 | print ( "There are more than five players on the server!" ) |
I hope this helps! If it answered your question, please select this as the correct answer so that others can benefit from it!