I'll write in snippets throughout the course of this answer and will integrate them at the end.
Well, first order of business, we need to be able to find the amount of players on a Team. As you likely know, the players on the team are not immediately accessible from the Team. Therefore, we could create a function as such:
1 | function findOnTeam(color) |
3 | for _,__ in next ,game.Players:players '' do |
4 | if (__.TeamColor = = color) then rawset (onteam,#onteam+ 1 ,__) end ; |
Now, we can find the players on a team. The next step is to hook to the all the Players a Changed event and balance the teams from there.
1 | game.Players.PlayerAdded:connect( function (plr) |
2 | plr.Changed:connect( function (prop) |
3 | if not (prop = = 'TeamColor' ) then return end ; |
4 | if (#findOnTeam(plr.TeamColor)> 1 ) then plr.TeamColor = BrickColor.new( 1 ) end ; |
Now, to integrate this:
01 | function findOnTeam(color) |
03 | for _,__ in next ,game.Players:players '' do |
04 | if (__.TeamColor = = color) then rawset (onteam,#onteam+ 1 ,__) end ; |
08 | game.Players.PlayerAdded:connect( function (plr) |
09 | plr.Changed:connect( function (prop) |
10 | if not (prop = = 'TeamColor' ) then return end ; |
11 | if (#findOnTeam(plr.TeamColor)> 1 ) then plr.TeamColor = BrickColor.new( 1 ) end ; |
Now, whenever a player's team changes, this check is performed. If two are already there, it sets them back to the White team.
EDIT: Do to popular demand, I've tabbed the code.