Auto Team players evenly

Hello I am a beginner Programer and I am making my first game! I am having trouble figuring out how I can automatically team players on a ‘Lobby Team’. I have three teams ‘Lobby Team’, ‘Blue Team’, and ‘Red Team’ I want to team players to ‘Red Team’ and ‘Blue Team’ Evenly Automatically.

I have been trying to get all the players in the game and give them a boolean value.

False = ‘Red Team,’ and True = ‘Blue Team’ But I don’t know how to give everyone a boolean value and how to use that value to switch their teams. (This is my first post so if I need to clarify below and I would be happy to respond.

How are players assigned to teams? Are players being automatically given a team when they join?

You can loop through the GetPlayers() method to get all the players. Then, you can assign them a team accordingly, by changing their Team property, and keep altering between the 2 teams, to make it even.

for i, plr in ipairs(game.Players:GetPlayers()) do
	if i%2 == 0 then -- this checks if the index is an even number
		plr.Team = game.Teams["Red Team"] -- if the index is even, assign them "Red Team"
	else
		plr.Team = game.Teams["Blue Team"] -- if the index is odd, assign them "Blue Team"
	end
end

Edit: to put players in the Lobby Team when they enter, just put Team.AutoAssignable on the Lobby Team to “true”

1 Like

I would create a function using the code that @FastKnight401 provided. You should then call it every time a player joins or leaves the game. This assures that the teams will automatically be evened.

It should look something like this.

function AutoTeam()
	for i, plr in ipairs(game.Players:GetPlayers()) do
		if i%2 == 0 then -- this checks if the index is an even number
			plr.Team = game.Teams["Red Team"] -- if the index is even, assign them "Red Team"
		else
			plr.Team = game.Teams["Blue Team"] -- if the index is odd, assign them "Blue Team"
		end
	end
end

game.Players.PlayerAdded:Connect(function() 
	AutoTeam()
end)

game.Players.PlayerRemoving:Connect(function() 
	AutoTeam()
end)
2 Likes

Let’s say there are 16 players on ‘Lobby Team’ and they are in a lobby when countdown hits zero they switch to their teams. I already have the countdown set and ready.

You should use the AutoTeam() function as soon as the countdown hits zero. If you need help doing this, reach out to me and I’ll do my best to help.

It wouldn’t be a good idea to use AutoTeam() in the PlayerRemoving function, as this could result in everyone getting moved to a different team if someone closer to the top left. (Someone with a name like 123Alex, or something.)

I think what the others also lack to mention is the difference between fairness and equality among players. Fairness is looking really at what makes your game “fair”, realistically that is balanced mechanics / teams. Equality is looking at how a player is treated comparatively to other players.

The issue with @CreditsMix 's solution is going to be the equality aspect, as teams can easily become imbalanced and players can be placed on losing teams when they are winning a game as the script literally randomizes the teams. A better method would be one which incorporates both new rounds whilst also handling players joining:

local GameRunning = false

function AutoTeam() -- We'll use his example for the auto teaming as its an efficient method.
	for i, Player in ipairs(game.Players:GetPlayers()) do
		if i%2 == 0 then -- this checks if the index is an even number
			Player.Team = game.Teams["Red Team"] -- if the index is even, assign them "Red Team"
		else
			Player.Team = game.Teams["Blue Team"] -- if the index is odd, assign them "Blue Team"
		end
	end
end

-- These next two statements exist for clarity purposes really, you can change them all you want but its to specify the game is running so we should treat some events differently.

function EndGame()
	GameRunning = false 
end

function BeginGame()
	GameRunning = true
	AutoTeam()
	-- WRITE THE THING BELOW YOURSELF.
	CountdownFunctionOrWhateverToControlBegin() -- this should probably also incorporate the game running statement so you can abruptly end games if needed, i'd use a general countdown though locked within it.
end

function BalancePlayer(Player)
	if #(game.Teams["Red Team"]:GetPlayers()) == #(game.Teams["Blue Team"]:GetPlayers()) then
		-- some team randomizer statement if teams are equal
		local number = math.random(1,2)
		if number == 1 then 
			Player.Team =  game.Teams["Red Team"] 
		else
			Player.Team =  game.Teams["Blue Team"] 
		end
	elseif #(game.Teams["Red Team"]:GetPlayers()) > #(game.Teams["Blue Team"]:GetPlayers()) then 
		-- Blue team has less players then red.
		Player.Team =  game.Teams["Blue Team"] 
	else
		-- Red team has less players then blue - only over occurrence we can get. 
		Player.Team =  game.Teams["Red Team"] 
	end
end

game.Players.PlayerAdded:Connect(function(Player) 
	if GameRunning then
		BalancePlayer(Player)
	end
end)

Using this method you then both recognize current players and the new player whilst also acknowledging if a round if running. You can also just do BeginGame() in your intermission statement.

Good luck scripting.

2 Likes