Okay, this can get a bit complex, so i'll try to explain all the necessary elements
Overview
We're going to be using a bit of math in this example, so if math isn't your subject, you should pay extra close attention. Now, to get the even amount of teams, we can basically sum it up in 1 equation:
Players % Teams
However, this equation is only valid if we use it correctly. The only reason this equation is relevant, is because of an array we'll be using.
Wait, what does the "%" do?
The % symbol (or, modulus) is an arithmetic operator that returns the remainder of 2 divided numbers. I'll explain why this can be useful later on.
What's an array?
An array is a table consisting of numeric keys. These numeric keys represent the value's position in the table, which makes it super easy for us to access them when using arithmetic operations.
When we index an array with a numeric key, it will return the value that key represents (as any kind of table would).
So, we can easily apply this useful knowledge to our equation:
02 | local _Teams = game:GetService 'Teams' |
03 | local _Players = game:GetService 'Players' |
08 | local Teams = _Teams:GetTeams() |
12 | local function GetPlayers() |
13 | return _Players:GetPlayers() |
18 | _Players.PlayerAdded:connect( function (Player) |
20 | Player.TeamColor = Teams [ #GetPlayers()%#Teams ] .TeamColor |
The "Teams" table
Now, since "Teams" is an array, we can index it with a numeric key. In this case, our numeric key being the remainder of #Players / #Teams.
It probably sounds like a bit much all at once, but i'd be happy to explain any questions you may have about this code. This will also work for any amount of teams, not just 2.