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

How to mae a last man standing type of thing?

Asked by 9 years ago

How do I make it so that when there is only 1 person left on a team then it runs some code? heres the code I tried but it doesn't work

if game.Teams.Survialists.NumPlayers == 1 then
-- code in here

Please answer ASAP

1 answer

Log in to vote
5
Answered by 9 years ago

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:

function getNumSurvivors()
     for i,plr in pairs(game.Players:GetPlayers()) do
       print(plr.Name .. " is a player!")
     end
end

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:

function getNumSurvivors()
local numPlrs = 0;
local team = game.Teams["Survivalists"]
for i,plr in pairs(game.Players:GetPlayers()) do
       print(plr.Name .. " is a player!")
end
return numPlrs;
end

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.

function getNumSurvivors()
local numPlrs = 0;
local team = game.Teams["Survivalists"]
for i,plr in pairs(game.Players:GetPlayers()) do
   if plr.TeamColor == team.TeamColor then
       numPlrs = numPlrs + 1;
  end
end
return numPlrs;
end

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.

function getNumSurvivors()
local numPlrs = 0;
local team = game.Teams["Survivalists"]
for i,plr in pairs(game.Players:GetPlayers()) do
   if plr.TeamColor == team.TeamColor then
       numPlrs = numPlrs + 1;
  end
end
return numPlrs;
end

repeat
numSurvivors = getNumSurvivors()
wait()
until numSurvivors == 1;
--code

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

Ad

Answer this question