How to count the amount of players alive on a team
4 replies, posted
this is what i got so far and i cant make it so i counts the alive ones
[CODE]
draw.DrawText("Props Alive: "..team.NumPlayers(TEAM_PROPS).."/", "shudfont2", ScrW()/1.38, ScrH()/1.04, Color(255, 255, 255, 255))
[/CODE]
i checked the wiki and i couldn't find it
I checked the wiki, and team.NumPlayers only seems to return the amount of players on a team, but you need to get the player's entity in order to check if they're alive.
Instead, loop through all players, check if they're alive, and which team they're on
[CODE]local amountAlive = 0
for k, ply in pairs( player.GetAll() ) do
if IsValid(ply) and ply:Team() == TEAM_PROPS and ply:Alive() then
amountAlive = amountAlive + 1
end
end
print("Props Alive: " .. amountAlive)[/CODE]
i tried it and it keeps counting to infinite!
[QUOTE='[PGN]Schuyler;49576019']i tried it and it keeps counting to infinite![/QUOTE]
Try put that outside the Paint function.
[lua]
frame.Paint = function(self, w, h)
local amountAlive = 0
for k, ply in pairs( player.GetAll() ) do
if IsValid(ply) and ply:Team() == TEAM_PROPS and ply:Alive() then -- if this is what you're are doing it's going to keep looping every frame and not work
amountAlive = amountAlive + 1
end
end
end[/lua]
if that dosen't work just make a function and return the number of alive players in the team
[lua]
function countAlivePlayers()
local amountAlive = 0
for k, ply in pairs( player.GetAll() ) do
if IsValid(ply) and ply:Team() == TEAM_PROPS and ply:Alive() then -- if this is what you're are doing it's going to keep looping every frame
amountAlive = amountAlive + 1
end
end
return amountAlive
end[/lua]
I got it i forgot to add a
amountAlive = 0
Sorry, you need to Log In to post a reply to this thread.