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

Problems with team balance script, ideas?

Asked by 8 years ago
local Fire = 0
local Ice = 0
local Math1 = math.rad(1,2)
game.Players.PlayerAdded:connect(function(Player)
    if Fire - Ice >=2 then
        Player.Neutral = false
        Player.TeamColor.BrickColor = BrickColor.new("Bright blue")
        Ice = Ice + 1
    end
    if Ice - Fire >= 2 then
        Player.Neutral = false
        Player.TeamColor.BrickColor = BrickColor.new("Bright red")
        Fire = Fire + 1
    end
    if Ice - Fire == 0 then
        if Math1 == 1 then
            Player.Neutral = false
            Player.TeamColor.BrickColor = BrickColor.new("Bright blue")
            Ice = Ice + 1
        else if Math1 == 2 then
            Player.Neutral = false
            Player.TeamColor.BrickColor = BrickColor.new("Bright red")
            Fire = Fire + 1
            end
        end
    end
end)

So what it does it keeps of how many players are on each team by the Fire, and Ice variables. And I think I got confused lines 7 and below is a bunch of math. And there are no errors in the output either. And it is supposed to be a team balance script, but I failed epicly. But the problem arises that it won't assign that Player to the team when the player joins. Ideas?

1 answer

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

Cleanup things:

  • If something happens in all branches, there's no reason put it in the if and the else. That means you should only need one line that says player.Neutral = false since that's what you always want
  • You should save variables for the two brick colors so that it's easier to change later and so that it's less typing.
  • math.rad does not pick random numbers. You want math.random.

Now, there's a problem immediately with your implementation. Fire and Ice won't be accurate, since you aren't adjusting them for when players leave (or when they switch teams, if there's some mechanism for that).

Instead of ticking them up or down by one, just get an accurate count based on current teamcolors at the beginning.

local FIRE = BrickColor.new("Bright red")
local ICE = BrickColor.new("Bright blue")

function getTeam(color)
    local t = {}
    for _, player in pairs(game.Players:GetPlayers()) do
        if player.TeamColor == color then
            table.insert(t, player)
        end
    end
    return t
end

function newPlayer(player)
    local fire = #getTeam(FIRE)
    local ice = #getTeam(ICE)
end

Now, what does "balanced teams" mean? If there's less on one team, put them on the team with less. If they're equal, put them on a random team.

More specifically:

  • if fire < ice, put on FIRE
  • if ice < fire, put on ICE
  • if fire == ice put on random team

Implementing that is very straightforward:

function newPlayer(player)
    local fire = #getTeam(FIRE)
    local ice = #getTeam(ICE)
    player.Neutral = false
    if fire < ice then
        player.TeamColor = FIRE
    elseif ice < fire then
        player.TeamColor = ICE
    else
        -- put on random team
        if math.random(1, 2) == 1 then
            player.TeamColor = FIRE
        else
            player.TeamColor = ICE
        end
    end
end
Ad

Answer this question