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

Team Randomizer help?

Asked by
Scootakip 299 Moderation Voter
8 years ago
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?

0
always randomly choose the odd numbered player and fill other team until its even ProfessorSev 220 — 8y

1 answer

Log in to vote
1
Answered by 8 years ago

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.

Ad

Answer this question