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

How can I split all players evenly into 2 teams?

Asked by 8 years ago

How would I split players evenly (or as evenly as you can if there is a odd number) into 2 teams. I know how to put people into teams and create them with a script. Just in not sure how to split all the players evenly.

Is it like for i = .... I'm not sure Please help!

?

3 answers

Log in to vote
1
Answered by 8 years ago

Okay, this can get a bit complex, so i'll try to explain all the necessary elements

Overview

We're going to be using a bit of math in this example, so if math isn't your subject, you should pay extra close attention. Now, to get the even amount of teams, we can basically sum it up in 1 equation:

Players % Teams

However, this equation is only valid if we use it correctly. The only reason this equation is relevant, is because of an array we'll be using.

Wait, what does the "%" do?

The % symbol (or, modulus) is an arithmetic operator that returns the remainder of 2 divided numbers. I'll explain why this can be useful later on.

What's an array?

An array is a table consisting of numeric keys. These numeric keys represent the value's position in the table, which makes it super easy for us to access them when using arithmetic operations.

When we index an array with a numeric key, it will return the value that key represents (as any kind of table would).

So, we can easily apply this useful knowledge to our equation:

-- Set up some services
local _Teams = game:GetService'Teams'
local _Players = game:GetService'Players'

-- Teams
-- Just gets all teams from the team service.
-- Note, this returns an ARRAY of teams. (thus, my explanation on arrays)
local Teams = _Teams:GetTeams()

-- Get players
-- Returns all players
local function GetPlayers()
    return _Players:GetPlayers()
end

-- New player
-- When the player joins...
_Players.PlayerAdded:connect(function(Player)
    -- We can use the modulus operator to return what team the player should go in
    Player.TeamColor = Teams[#GetPlayers()%#Teams].TeamColor
end)

The "Teams" table

Now, since "Teams" is an array, we can index it with a numeric key. In this case, our numeric key being the remainder of #Players / #Teams.

It probably sounds like a bit much all at once, but i'd be happy to explain any questions you may have about this code. This will also work for any amount of teams, not just 2.

0
Ok and if I were to change it from PlayerAdded function to like scramble I could call it with another function when ever I want. Right? Bennymax3333 0 — 8y
0
That would be a different procedure. However, this should suffice for automatically evening all players to teams. CodingEvolution 490 — 8y
Ad
Log in to vote
0
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
8 years ago

Well, personally, I'd create 3 tables. 1 for Team 1, 1 for Team 2, and 1 for players. Since you didn't prove any attempts at scripting this, I'll provide you some tips. Create a function to determine if there is an even amount of players, or not. With that, you can either split the players directly in half, or randomize it to your liking. As you choose them, remove them from the Players table, into a team table. After one team is settled, move the remaining players in the table, to team 2. After your tables are complete, use a simple for loop, to team them all to the corresponding team. There you go!

Log in to vote
0
Answered by 8 years ago

Problem You need to put some scipt in it here you go though

Solution

function scramble(team1color,team2color) -- the variables need to be like "Black" or white"
team1 = {}
team2 = {}
check = 0
for i,v in pairs(game.Players:GetChildren())
if check == 0 then
table.Insert(team1,#team1+1,v)
check =1
end
if check == 1 then
table.Insert(team1,#team2+1,v)
check =0
end
end

for i,v in pairs(team1) do
v.TeamColor = BrickColor.new(team1color)
end
for i,v in pairs(team2) do
v.TeamColor = BrickColor.new(team2color)
end
end

scramble("Black","White")

tell me if this works, if not just message me the error

0
Where it says function scramble(team1color,team2color) do you put the team color (like white or bright blue) after the team1 or replace the whole thing with the color Bennymax3333 0 — 8y
0
You should make those variables local to the scope. CodingEvolution 490 — 8y
0
What do you mean local to the scope Bennymax3333 0 — 8y
0
I'll post a sufficient answer. CodingEvolution 490 — 8y

Answer this question