Team Restrictions

is there any way to restrict a team if it is too full?
such as there is 2 reds and one blue, and you want to join red, but you cant. since theres too many. so you have to join blue instead so that a certain team doesnt get a ton while the other has like none

**[Team.NumPlayers

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Team.NumPlayers)**

In your team joining function you could add something like this:
(presuming the red team has the index 5 and the max number of players on it is 6…)
[lua]
if Team.NumPlayers(5) < 6 then
– your code to join the team here…
else
return
end
[/lua]

the team indexes are:
Blue (team_2) index 2
red (team_1) index 1

all this would do is make the player instantly spawn on the other team. im looking for a tf2 like team balance. so it prints on the HUD that the red team is too full, and you have to join the other meanwhile its rejecting the players choice, so the player must choose blue.
as long as one team is greater than the other, they must choose the lesser team.

Save this in a Lua file in garrysmod/lua/autorun:

[lua]
if CLIENT then return end

if SERVER then

function SetTeamOnJoin( ply )
if Team.NumPlayers( 2 ) > Team.NumPlayers( 1 ) then
ply:SetTeam( 1 )
ply:PrintMessage( HUD_PRINTCENTER, “Welcome to the server, you have been placed on team red!” )
elseif Team.NumPlayers( 1 ) > Team.NumPlayers( 2 ) then
ply:SetTeam( 2 )
ply:PrintMessage( HUD_PRINTCENTER, “Welcome to the server, you have been placed on team blue!” )
elseif Team.NumPlayers( 1 ) == Team.NumPlayers( 2 ) then
ply:SetTeam( 1 )
ply:PrintMessage( HUD_PRINTCENTER, “Welcome to the server, you have been placed on team red!” )
end
end
hook.Add( “PlayerInitialSpawn”, “SetTeamOnJoin”, SetTeamOnJoin )

function CheckTheTeams()
local red = Team.NumPlayers( 1 )
local blue = Team.NumPlayers( 2 )
if ( red - blue ) => 2 then
local numplayers = #player.GetAll()
local plyid = math.random( 1, numplayers )
for k, v in pairs(player.GetAll()) do
if k == plyid then
v:SetTeam( 2 )
v:PrintMessage( HUD_PRINTCENTER, “The teams are un-balanced! You have been randomly selected to join team blue!” )
end
end
elseif ( blue - red ) => 2 then
local numplayers = #player.GetAll()
local plyid = math.random( 1, numplayers )
for k, v in pairs(player.GetAll()) do
if k == plyid then
v:SetTeam( 1 )
v:PrintMessage( HUD_PRINTCENTER, “The teams are un-balanced! You have been randomly selected to join team red!” )
end
end
end
end
hook.Add( “Think”, “CheckTheTeams”, CheckTheTeams )

end
[/lua]

This would make my VGUI that i made to appear on initial spawn obsolete and useless, i need a code that dissallows the joining of a team if it is larger than the other.