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

How can I execute a script to split players into evenly matched teams?

Asked by 10 years ago

The teams are blue and red i want them to be split into even teams at a certain point during the game (not at the beggining)I have tried everything I know to do this one script was successful but it did not evenly split the teams can someone please help.

1 answer

Log in to vote
0
Answered by
jobro13 980 Moderation Voter
10 years ago

I understand "even teams" as teams with the same amount of members. If there is an uneven amount, one team will have one more member than the other team.

function Rebalance()
    local teams = {game.Teams.Red, game.Teams.Blue}
    if math.random(1,2) == 1 then
        teams = {game.Teams.Blue, game.Teams.Red} -- Otherwise always the red team has 1+ member
    end
    local plist = game.Players:GetPlayers()
    local function random_player()
        local new = math.random(1, #plist)
        local player = plist[new]
        table.remove(plist, new)
        return player
    end
    for i = 1, #plist do 
        local new player = random_player
        new_player.TeamColor = teams[(i % 2) + 1]
    end
end

Just call this function at any given moment.

Please note that players which join at the game spawn will not be added to any of those teams - you need to put them in manually. You haven't given any behavior for this so I cannot define that for you... :)

Ad

Answer this question