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

How to create randomly generated teams?

I need to make a function that will put 4 people on the Bright red team, 2 on the Bright blue team, and the rest on the Bright green team. How would I go about selecting these players randomly and putting them on these teams? I have no idea how to do this, so I will not supply any code as I know it wont help with the process.

2 answers

local brightRed = BrickColor.new("Bright red")
local brightBlue = BrickColor.new("Bright blue")
local brightGreen = BrickColor.new("Bright green")
if game.Players.NumPlayers > 6 then
	for i = 1, #game.Players:GetPlayers() do
		if i <= 4 then
			game.Players[game.Players:GetPlayers()[i]].TeamColor = brightRed
		elseif i > 4 and i <=6 then
			game.Players[game.Players:GetPlayers()[i]].TeamColor = brightBlue
		else
			game.Players[game.Players:GetPlayers()[i]].TeamColor = brightGreen
		end
	end
end
local minimum_plrs = 6
local red = BrickColor.new("Really red")
local blue = BrickColor.new("Bright blue")
local green = BrickColor.new("Bright green")


if game.Players.NumPlayers >= minimum_plrs then
	local already_chosen = {}  
  	 local color
	local players = game.Players:GetPlayers()
	for i = 1, #players do 
		local randy 
   	 	color = (i > 6) and green or (i <= 2) and blue or red
		repeat randy = players[math.random(1, #players)]  wait() until not already_chosen[randy.Name]
		already_chosen[randy.Name] = true
   		randy.TeamColor = color or BrickColor.White()
	end
end

ternary operator's make things shorter and are extremely useful. They're kind of difficult to understand, but this is basically what I'm saying;

color = (i > 6) and green or (i <= 2) and blue or red

if i > 6 then -- more than 6
	color = green
elseif i <= 2 then -- less than or = 2
	color = blue
else
	color = red -- more than 2, less than 6.
end
-- Due to concerns in the comments, you can use this to test it.

local minimum_plrs = 6
local red = BrickColor.new("Really red")
local blue = BrickColor.new("Bright blue")
local green = BrickColor.new("Bright green")
local test = Workspace.Model

--if game.Players.NumPlayers >= minimum_plrs then
	local already_chosen = {}  
    local color
	local players = test:GetChildren()
	for i = 1, #players do 
		local randy 
   		 color = (i > 6) and green or (i <= 2) and blue or red
		repeat randy = players[math.random(1, #players)]  wait() until not already_chosen[randy.Name]
		already_chosen[randy.Name] = true
   		randy.BrickColor = color or BrickColor.White()
	end
--end


4 comments

0

I'm kinda confused, are you making it so 4 people turn green, 2 people turn blue, then the rest turn red? I don't see a 4, so I'm kinda confused. Lol.

0

it covers everything from 1,2 else is for 4 and > 6 = green, which is everyone else. Also you said; 4 people on the Bright red team, 2 on the Bright blue team, and the rest on the Bright green team

0

Yea I said that, but I though you were making it different. Also, I got a problem, on the leaderboard it says I'm on the Bright blue team but on my Team checker it says I'm on the Red team. Also, can I test it by myself or do I need 6 people?

0

I added something so you can test it, read the comment in the script and fill the model with more than 6 bricks, like 15 or more w/e. I've also had problems with the scripts saying I'm on one team and ROBLOX saying another, I think it's a bug.

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