The function you want would probably be to auto assign players to a list of teams.
Its declaration would look like this:
function autoAssignTeams( teams )
Where teams will be a list of Brick Color names.
We can see pretty clearly what to get as the body of the function from what you posted:
for i,v in pairs(Game.Players:GetPlayers()) do
if i ~= #game.Players:GetPlayers() and #game.Players:GetPlayers() % #teams ~= 0 then
v.TeamColor = BrickColor.new( teams[(i%#teams)+1] )
else
v.TeamColor = BrickColor.new( teams[math.random(#teams)] )
end
end
So we just wrap this into our functoin:
function autoAssignTeams( teams )
for i,v in pairs(Game.Players:GetPlayers()) do
if i ~= #game.Players:GetPlayers() and #game.Players:GetPlayers() % #teams ~= 0 then
v.TeamColor = BrickColor.new( teams[(i%#teams)+1] )
else
v.TeamColor = BrickColor.new( teams[math.random(#teams)] )
end
end
end
This is how you would use this function to accomplish the same thing as the snippet you posted in your question:
while true do
wait(1)
autoAssignTeams( {"Bright yellow", "Dark stone grey"} )
wait(3)
autoAssignTeams( {"White"} )
end
A small note: the implementation is wrong if you want teams to be as even as possible with more than two teams. The i ~= #game.Players:GetPlayers() only applies (in general) as a sufficient check for 2 input teams -- more may be balanced wrong. The solution to this is not very easy, though.
There was also a mistake in the above; it should be using and instead of or