Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

I need help randomly teaming players?

Asked by 4 years ago

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!

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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.

Ad

Answer this question