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

How do I randomize teams in Roblox?

Yeah, I've never been good with randomizing, but this is what I've got but it doesn't work?

local function SplitPlayers()
local players = {}
for _, player in pairs(game.Players:GetPlayers()) do
if player.TeamColor == BrickColor.new("Medium stone grey") then
table.insert(players, player)
end
end

local redTable = {}
local blueTable = {}
local redPlayers = math.floor(1/2 + #players * 4/6)
local bluePlayers = #players - redPlayers

for i = 1, redPlayers do
local player = table.remove(players, math.random(#players))
table.insert(redTable, player)
player.TeamColor = BrickColor.new("Really red") 
end

for i = 1, bluePlayers do
local player = table.remove(players, math.random(#players))
table.insert(blueTable, player)
player.TeamColor = BrickColor.new("Bright blue")
end
end

3 answers

local teams = {"Bright blue","Bright red"}

local number = 1
	
    for _, player in pairs(game.Players:GetChildren()) do
		    	player.TeamColor = BrickColor.new(teams[number])
		    	number = number + 1
	    		if number > #teams then
	 		number = 1
		   end
    end

Change what's inside the teams table to the team color you want, just make sure they are BrickColor's.

4 comments

0

Please explain your answer. Simply code is not enough.

0

Wow downvote me for giving him the answer e.e

0

Yes it's an answer but this will not help him learn unless he understands what this does and how it works, and most likely wont as he didn't make it like this in the first place. Explain code, don't just post it and say "Here you go"

0

Yes, I agree with ChemicalHex and MrLegoman900, you need to explain your answer. Even I couldn't understand it at first. And make sure you format your code properly, it's a bit wonky.

wait(10) -- Change 10 to the amount of time you want this to happen
for i, v in pairs(game.Teams["White Team"]:GetPlayers())do
v.Character.Head:Destroy()
end

Change White Team to the team were you will get all the players. Might not be a team randomizer but it works.

2 comments

0

Make it a script, Put it in ServerScriptService

0

i did it but it said this in output: ServerScriptService.Script:2: bad argument #1 to '(for generator)' (table expected, got nil)

I have the perfect idea! I write script only in ServerScriptService:

local teams = {'Really red', 'Really blue'}
local team = {{''},{''}} -- Red and Blue team

game.Players.PlayerAdded:Connect(function(plr) -- Add player in random team
	local plrs = game.Players:GetChildren()
	if #team[1] == #team[2] then
		local choosed = math.random(1,2)
		table.insert(team, choosed, plr)
		plr.TeamColor = BrickColor.new(teams[choosed])
	elseif #team[1] < #team[2] then
		table.insert(team, 1, plr)
		plr.TeamColor = BrickColor.new(teams[1])
	elseif #team[1] > #team[2] then
		table.insert(team, 2, plr)
		plr.TeamColor = BrickColor.new(teams[2])
	end
end)

game.Players.PlayerRemoving:Connect(function(plr) -- Remove player team
	if plr.TeamColor == 'Really red' then
		for i = 2, #team[1] do
			if team[1][i] == plr.Name then
				table.remove(team, 1, i)
				break
			end
		end
	else
		for i = 2, #team[2] do
			if team[2][i] == plr.Name then
				table.remove(team, 2, i)
				break
			end
		end
	end
end)

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