I made a random team assigning script, which chooses a number from 1 to 4. I have an if else statement for every available scenario. The problem is, sometimes the numbers are the same. For example, lets say person A got the number 1. Then, the person B also gets the number 1 which causes chaos, especially when all players are teamed as the same team. Any suggestions?
Code:
local plrs = game.Players:GetChildren()
--Team randomizer
for i = 1,#plrs do
local TeamsL = math.random(1,4)
if TeamsL == 1 then
plrs[i].TeamColor = BrickColor.new("White")
plrs[i]:LoadCharacter()
elseif TeamsL == 2 then
plrs[i].TeamColor = BrickColor.new("Dark green")
plrs[i]:LoadCharacter()
elseif TeamsL == 3 then
plrs[i].TeamColor = BrickColor.new("Bright blue")
plrs[i]:LoadCharacter()
elseif TeamsL == 4 then
plrs[i].TeamColor = BrickColor.new("Bright red")
end
You want to cycle through a table that refreshes every four times. Pop the team picked in the table every iteration to avoid unbalanced numbers per team, if you decide to keep the random functionality.
Otherwise, you can pick random players out of the player table and then assign to one team in order.
That is how the functionality intends to works. The smaller the range, the greater the chance of getting duplicates. If you wanted to prevent this, you could set the range to a larger number and works with conditional checks around that, or you could keep track of the your generated numbers and re-generate until your random number is not equal to any of the listed numbers.
This is pretty interesting choice of yours to make sure that each of the teams get an equal amount of players, oh well, I tried making something that generates unrepeated numbers by adding the already existing number in a table and loop the function itself, if the table found all of the numbers it reset. (Just like what @anon81993163 said yes)
local Repeated = {}
for i = 1, 6 do
local SelectedTeam
local function PickRandomNumber()
local Generated = math.random(1, 4)
if not table.find(Repeated, Generated) then
table.insert(Repeated, Generated)
SelectedTeam = Generated
elseif #Repeated < 4 then
-- Repeated result, table is still not 4 yet, doing it again.
PickRandomNumber()
else
Repeated = {}
-- Reset table for the balanced team since all of the first
-- four players has been selected.
PickRandomNumber()
end
end
PickRandomNumber()
print(SelectedTeam)
end
The output should be something looking like this since the function ran 6 times.
Implement this within your code is going to look like this.
local Players = game.Players:GetChildren()
local Repeated = {}
for i = 1, #Players do
local SelectedTeam
local function PickRandomNumber()
local Generated = math.random(1, 4)
if not table.find(Repeated, Generated) then
table.insert(Repeated, Generated)
SelectedTeam = Generated
elseif #Repeated < 4 then
-- Repeated result, table is still not 4 yet, doing it again.
PickRandomNumber()
else
Repeated = {}
-- Table is full of all possible numbers, resetting the table, doing it again.
PickRandomNumber()
end
end
PickRandomNumber() -- Run the function once
if SelectedTeam == 1 then
Players[i].TeamColor = BrickColor.new("White")
Players[i]:LoadCharacter()
elseif SelectedTeam == 2 then
Players[i].TeamColor = BrickColor.new("Dark green")
Players[i]:LoadCharacter()
elseif SelectedTeam == 3 then
Players[i].TeamColor = BrickColor.new("Bright blue")
Players[i]:LoadCharacter()
elseif SelectedTeam == 4 then
Players[i].TeamColor = BrickColor.new("Bright red")
end
end