Need help finding a way to tally up all living players per team to seperate tables

Hello everyone. I need some help.

What am I trying to do? -

I want to output an integer of how many people are alive for a specific team, team1 and team2.

Problem -

I’ve scoured the internet/this site/google for hours, and ended up at the same dead-ends.

Attempts -

I know GAMEMODE:GetTeamAliveCounts() exists, but that outputs who is alive for both teams like


1  =  2
2  =  4

That output is an unusable integer unfortunately.

Curiously, the following would always output the number “2”. Probably the last integer in the table.


MsgAll(table.Count(GAMEMODE:GetTeamAliveCounts()))

Just out of curiosity, I wanted to see what was in the GAMEMODE:GetTeamAliveCounts() exactly, perhaps something usable. Not exactly. It changed every second.


MsgAll(GAMEMODE:GetTeamAliveCounts())

Bot58 suicided!
table: 0x17c5ad08
table: 0x133b1608


Doing something like the below doesn’t work at all, and according to all sources shouldn’t.


local Teams1Alive = GAMEMODE:GetTeamAliveCounts(TEAM_ONE)
local Teams2Alive = GAMEMODE:GetTeamAliveCounts(TEAM_TWO)

Next steps after this -
Once this is all figured out, I plan to use the number of alive players for a specific team to do specific things.

For example, this would play a sound saying, “Oh no, two players left.”


if table.getn(Team1Alive) == 2 then
pl:SendLua('surface.PlaySound("announcer/remaining/2left.mp3")')	
end

Perhaps the answer lies within


for _, pl in pairs(player.GetAll()) do

and


ply:Alive()

but I can’t figure that out either. Getting everyone that is alive is easy, but it seems difficult when you want to separate it into two teams/two tables.

I finally come to the end of this post and wish there was a GAMEMODE:GetAliveCountPerTeam, but I don’t believe it exists. How can I accomplish this my LUA master superiors? :slight_smile:

GetTeamAliveCounts is not on the wiki so no idea what is, but you can just get all the players on the team with team.GetPlayers() and then check who is alive.

[lua]local function GetNumberOfPlayersAliveOnTeam(teamIndex)
local alive = 0
for k,v in pairs(team.GetPlayers(teamIndex)) do
if v:Alive() then alive = alive+1 end
end
return alive
end

local Team1Alive = GetNumberOfPlayersAliveOnTeam(1)
local Team2Alive = GetNumberOfPlayersAliveOnTeam(2)[/lua]

That worked great! Thank you very much! The only issue, if it really is one, is that the message repeats whenever a player joins or leaves the server on either team. Any idea how to curb that?