Hey, how would I go about randomly teaming everyone to red and blue? I know how to team the player and everything, I just need to know how to make the teams even. I would appreciate anyone's help greatly!
✓ ANSWER ACCEPTED
I need help randomly teaming players?
1 answer
✓ Accepted answer
If you want to split the teams evenly, and randomly each time, you can do the following:
local Teams = game:GetService("Teams")
local redTeam = Teams.Red
local blueTeam = Teams.Blue
-- Function to shuffle a table, found here: https://gist.github.com/Uradamus/10323382
local function Shuffle(tbl)
for i = #tbl, 2, -1 do
local j = math.random(i)
tbl[i], tbl[j] = tbl[j], tbl[i]
end
return tbl
end
local players = game.Players:GetPlayers()
players = Shuffle(players)
for i = 1, #players do
local player = players[i]
-- Set every odd number to the blue team, even to red
local team = i % 2 == 0 and redTeam or blueTeam
player.Team = team
player.TeamColor = team.TeamColor
end
If you want the split to be the same every time you can remove the shuffle.
This discussion is preserved as it was. New answers, comments, and votes are closed.
