Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Problems with team balance script, ideas?

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

✓ Accepted answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.