Hi, I would like to know how can I create a balanced-team assigning system?
So let’s say there’s 12 players and a team game mode happens, how can i make it so there is a decently fair amount of players on each team, so in this case 6 on blue team and 6 on red?
And if there’s 7 players then like 4 on a team and 3 on a team?
Just would like someone to show me a way to create a balanced assigning function, thanks
local players ={"Player1"; "Player2"; "Player3"; "Player4"; "Player5"} -- Replace with the actual players
for i, player in pairs(players) do
print(player, "is on team", (i%2)+1)
end
That will loop through the players, putting the player on the other team each time (i.e. player1 on team1, player2 on team2, player3 on team1, player4 on team2 …)
Also, you of course don’t want to just print it out, but actually put the player on a team.
Adding onto @Orbular3’s code, you can also try this here if you are trying to get it random completely.
local readytogo = lobbyteam:GetPlayers()
local currentMax = #readytogo
local function AssignRandomly()
for i = currentMax,currentMax/2,-1 do
local randomPlayer = readytogo[math.random(1, currentMax)]
randomPlayer.Team = redteam
end
for i,remain in pairs(lobbyteam:GetPlayers()) do
remain.Team = blueteam
end
end
AssignRandomly()
If there are some mistakes in the script, my apologies I tested it with bricks instead of players.
Thank you, can you explain to me what this code does? does it assign players to a team in a balanced way? so if there’s 10 players then it’s 5 on blue and 5 on red, and if there’s 7 players then 4 on one team and 3 on another team?
By balanced do you just mean even numbers or by skill also. Im pretty sure you can assign even numbers on your own but if you dont just run a check every few minute to see if the plyr count is balanced. For skill youd need a way of measuring skill based on the game ur making. FPS games use kdr (kills/deaths) and try to get the average on each team the same
You can use a function like this. It will get the teams and players in the game. Then it will assign a near-equal amount of players to each team. The reason it can’t be perfectly equal is because there might be an un-even amount of players in the game.
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local function RandomizeTeams()
local players = Players:GetPlayers()
local teams = Teams:GetTeams()
local function assignLeftoverTeams()
for _,player in pairs(players) do
local teamIndex = math.random(#teams) -- get random team
local team = teams[teamIndex]
player.Team = team
table.remove(teams, teamIndex)
end
end
if #teams >= #players then
assignLeftoverTeams()
else
local splitAmount = (#players-(#players % #teams)) / #teams -- the amount to split the players by
for team in pairs(teams) do -- assign the players evenly to each team
for i=1,splitAmount do
local playerIndex = math.random(#players)
local player = players[playerIndex]
player.Team = team
table.remove(players, playerIndex)
end
end
assignLeftoverTeams()
end
end