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

How to make a team Auto Balance script?

Asked by 10 years ago

How would I go about makeing a Team balace script.

team1 = Game.Teams.team1 team2 = Game.Teams.Team2

How can I find the number of players in a team

team1num = 0 team2num = 0

if team1.PlayerAdded then team1num +1

if team2.PlayerAdded then team2num +1

if team1num >= team2num then Player.teamColor = BrickColor.new("team2Color") if team2num >= team1num then Player.TeamColor = BrickColor.new("team1Color")

^^^^ Is that anywhere close to what im looking for?

1 answer

Log in to vote
3
Answered by
nate890 495 Moderation Voter
10 years ago

The teams service should automatically (try to) balance teams when players join but if you wanted to rebalance teams you could write your own function to handle that. Take a look at this wiki page that shows how to rebalance teams -I don't think the actual method works though but it wouldn't hurt to try.

However, to answer your question fully,

function getPlayersOnTeam(teamColor)
    local players = {}
    for _, p in pairs(game.Players:GetPlayers()) do
        if p.TeamColor == BrickColor.new(teamColor) then
            table.insert(players, p)
        end
    end
    return players
end

function getSmallestTeam(teams)
    local numPlayers, smallestTeam = math.huge
    for team, players in pairs(teams) do
        if #players < numPlayers then
            numPlayers = #players
            smallestTeam = team
        end
    end
    return smallestTeam
end

game.Players.PlayerAdded:connect(function(player)
    local reds = getPlayersOnTeam("Bright red")
    local blues = getPlayersOnTeam("Bright blue")
    local greens = getPlayersOnTeam("Bright green")
    local smallestTeam = getSmallestTeam({
        ["Bright red"] = reds,
        ["Bright blue"] = blues,
        ["Bright green"] = greens
    })
    player.TeamColor = BrickColor.new(smallestTeam)
end)

Whenever a player joins, the code above will put them on the smallest team out of the Bright red, Bright blue and Bright green teams.

0
Awsome Thank you, Really helped me. twizon69 15 — 10y
0
Same LordTechet 53 — 7y
0
Same LordTechet 53 — 7y
0
Same LordTechet 53 — 7y
Ad

Answer this question