Team Spawning Help

Say that in my gamemode, some maps have different numbers of teams (ranging from 2 to 4 team maps). Some are red vs blue, some are yellow vs blue, red vs green, red vs blue vs yellow, red vs blue vs green vs yellow, and so on. How would I go about implementing a way to balance these teams on PlayerInitialSpawn, in a way that will work for any combination of these teams? I tried the following code but it does not work and instead will sometimes put a player on green team if info_player_blue and info_player_red exists. Is there possibly some way to check if a map has the info_player_red entity, for example? Any help would be appreciated


		if team.NumPlayers(TEAM_YELLOW) <= team.NumPlayers(TEAM_GREEN) then
			ply:SetTeam(TEAM_YELLOW)
			elseif team.NumPlayers(TEAM_GREEN) <= team.NumPlayers(TEAM_YELLOW) then
			ply:SetTeam(TEAM_GREEN)
		end
		if team.NumPlayers(TEAM_RED) <= team.NumPlayers(TEAM_BLUE) then
			ply:SetTeam(TEAM_RED)
			elseif team.NumPlayers(TEAM_BLUE) <= team.NumPlayers(TEAM_RED) then
			ply:SetTeam(TEAM_BLUE)		
		end
		if team.NumPlayers(TEAM_GREEN) <= team.NumPlayers(TEAM_BLUE) then
			ply:SetTeam(TEAM_GREEN)
			elseif team.NumPlayers(TEAM_BLUE) <= team.NumPlayers(TEAM_GREEN) then
			ply:SetTeam(TEAM_BLUE)		
		end

If I understood correctly, you want to evenly distribute players over a dynamic amount of teams? In that case, you could make a function that returns the team with the least players where the parameter is a vararg containing the teams. Use this to set the player’s team on the PlayerInitialSpawn hook. You could store all teams that you made in a global table, and use that to find out what teams exist.

Thanks for the reply, I should say what’s really giving me trouble in this is checking what teams exist in a certain map. What’s a good way to check if the info_player_blue or info_player_red entities will exist in a given map?