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

Team Randomizer help?

function getPlayerNumberInTeam(TeamColor)
local num = 0
for index,player in pairs(game.Players:GetChildren()) do
if tostring(player.TeamColor) == TeamColor and not player.Neutral then
num = num+1
end
end
return num
end

This script is made to put players into equal 2 teams. The issue is that the teams are always the same. Is there any way to randomize the teams a bit more while also keeping them as balanced as possible?

1 comment

0

always randomly choose the odd numbered player and fill other team until its even

1 answer

Well, having the whole randomness of the team selection kinda contradicts the ability to keep it balanced.

If you want a better way to organize your players into different teams in a balanced manner, here's some code I whipped up:

-- Get our services responsible
local TeamService = game:GetService'Teams'
local Players = game:GetService'Players'

-- Do this calculation when the player joins
Players.PlayerAdded:connect(function(Player)

	-- Get our player / team numbers
	local NumPlayers = Players.NumPlayers
	local Teams = #TeamService:GetTeams()

	-- Assort the player to the balanced team
	Player.TeamColor = TeamService[NumPlayers%Teams == 0 and Teams or NumPlayers%Teams].TeamColor

end)

How it works

Basically all we're doing here is getting the remainder of #players / #teams, and assorting the player to that index in GetTeams

Sorry if this wasn't quite the answer you were looking for, but this is just based off what I understood from your question. If this wasn't exactly what you had in mind, leave a comment and I'll update my answer to whatever questions / clarifications you'd like to remark.

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