Getting Team Player Count
What you're doing wrong: First off, you are trying to read from the NumPlayers property. This is an issue because NumPlayers does not exist in teams, as it is a property unique to the Player Service and nothing more.
If you take a look at the properties of a team instance, you'll notice that there is no property that gives you the number of players that may be on that team. To solve this issue, we must loop through the amount of Players and compare their teamColor to add to a specific count. Since the team you are looking for is called 'survivalists', I will focus on that.
First off, let's design a loop to go through all the players in the current game. We will put this in a function to ensure we can run the code anytime we want to. For simple terms, we will name the function getNumSurvivors
:
1 | function getNumSurvivors() |
2 | for i,plr in pairs (game.Players:GetPlayers()) do |
3 | print (plr.Name .. " is a player!" ) |
Great! That wasn't so bad. Now let's set up some variables that are designated to help us in our checking of which player is on the survival team. Of course we first need the compiler to know which team we want to check the number of players for:
1 | function getNumSurvivors() |
3 | local team = game.Teams [ "Survivalists" ] |
4 | for i,plr in pairs (game.Players:GetPlayers()) do |
5 | print (plr.Name .. " is a player!" ) |
We are now at the end of what we must do! All thats left is to check which Players have the same TeamColor as the Survivors team color! To do this, we just put in a conditional and increment [increase] numPlrs
based on the results.
01 | function getNumSurvivors() |
03 | local team = game.Teams [ "Survivalists" ] |
04 | for i,plr in pairs (game.Players:GetPlayers()) do |
05 | if plr.TeamColor = = team.TeamColor then |
06 | numPlrs = numPlrs + 1 ; |
DING!
The function is now done! Now we must come back to your question, How do I make it so that when there is only 1 person left on a team then it runs some code?
To do this, we can constantly loop the code until only one player remains. Remember that getNumSurvivors returns the exact number of Players on a team.
01 | function getNumSurvivors() |
03 | local team = game.Teams [ "Survivalists" ] |
04 | for i,plr in pairs (game.Players:GetPlayers()) do |
05 | if plr.TeamColor = = team.TeamColor then |
06 | numPlrs = numPlrs + 1 ; |
13 | numSurvivors = getNumSurvivors() |
15 | until numSurvivors = = 1 ; |
At this point, the code has ensured us that only one survivor is left. Now, we can easily just run code right under this section as noted with '--code'.
Useful Links
Team: http://wiki.roblox.com/index.php?title=API:Class/Team
Conditional Statements: http://wiki.roblox.com/index.php?title=Conditional_statement
Loops: http://wiki.roblox.com/index.php?title=Loops
Functions: http://wiki.roblox.com/index.php?title=Function