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

How could I create randomized teams?

Asked by 4 years ago
local Teams = {

    Group1 = {},

    Group2 = {},

    Group3 = {}
}

Problem

I have this table ( above ) called teams that holds 3 groups. My plan is to have these groups hold a randomized set of players in a round sequence. ( new round = new set of groups ) The biggest issue I've been having is making each group hold a max of 3 players in each one, while also making sure that each group is filled 1 by 1 ( <= reason being in the case of there only being 3 players then they'll still be in seperate groups against each other )

I've been doing a lot of searching around and looking at tons of examples, but I haven't really found anything that would help me in figuring this out. The farthest I've gotten is looping through the current players in game and putting them in one of the groups. Don't have much of a clue on organizing them how I want.

I know I can figure this out, but a little direction or something to get me started would help me tremendously.

Question

How could I make randomized groups of 3? And if possible, also make each team/group is filled in 1 by 1.

side note

I don't know too much of the benefits of using the team service, but I'm avoiding using it for specific reasons.

1 answer

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

Try something like this:

local Teams = {
    Group1 = {},

    Group2 = {}.

    Group3 = {}
}

local Players = game:GetService("Players")
local playerList = Players:GetPlayers()

-- Randomize the player list
for i = #playerList, 2, -1 do
    local rand = math.random(i)

    playerList[i], playerList[rand] = playerList[rand], playerList[i]
end

-- For each player
for i = 1, #playerList do
    local groupNumber = (i - 1) % 3 + 1 -- Get a number between 1-3 depending on the remainder of the number in the list
    table.insert(Teams["Group" .. groupNumber], playerList[i]) -- Add that player to that group
end
0
Thanks so much EstrangedFisherman 51 — 4y
Ad

Answer this question