How to tell if the same amount of people from each team are in an ents.FindInBox

Hey there!

I’m sorta slumped on this, and I can’t figure this out.

I’m working on control points in my gamemode. In most control point based FPSs, when the same amount of players from each team enters a control point, it becomes neutral.

However, I can’t figure out how to tell the system how to compare the two in the box.

Any help would be appreciated!



for k, v in pairs(ents.FindInBox( Vector(-195.968750, -48.005539, 120.031250), Vector(219.968750, -399.968750, 256.031250) ) ) do
 Cscoresmooth = math.Approach(Cscoresmooth, Cscore, FrameTime())
		if v:IsPlayer() and v:Team() == 1 and Ccaptured == false then...
   --preform action for one team here
                elseif v:IsPlayer() and v:Team() == 0 and Ccaptured == false then...
  --preform action for other team here

Outside the loop you can make two variables: one for blue players and one for red players.
In your loop check if the entity is a player; if yes, then check if their team is blue or red, then increment the blue/red player count variables defined outside the loop accordingly. Some example code:

bluePlayers = 0
redPlayers = 0

for _, v in pairs (entsInBox) do
if v:IsPlayer() then
if v:Team() == TEAM_BLUE then
bluePlayers = bluePlayers + 1
elseif v:Team() == TEAM_RED then
redPlayers = redPlayers + 1

From there you can easily compare the two values to see if the zone is contested or not. You may also want to check if the player in the zone is alive or not.

If you’re just finding players in a box, it’s more efficient to iterate over the players and check if their position is in a box, rather than using ents.FindInBox as that iterates over every entity.

^^^ Word.

Using ents.FindInBox will not just find the players, but also all of their weapons for example, if you have 15 weapons on each player and there are 5 players in the box and 10 players in the server, it’s better to look if the 10 players are in the box than checking if the 75 entities are players.

Loop through all the players and use this:

Also, it may be useful to put the players in a table of the people of each team, and then just get the size of the table, if you plan on doing anything with the specific players in it. Otherwise, of course, just use the number