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

Random teams not so random, everyone joining red?

function getTeams()
	local teamNumber = 1
	repeat wait()
		local lobby = {}
		local teams = {"Red", "Blue"}
		for _, player in pairs(game.Players:GetChildren()) do
			if player.TeamColor == game.Teams["Lobby"].TeamColor then
				local character = player.Character
				if character and character:FindFirstChild("Torso") then
					table.insert(lobby, player)
				end
			end
		end
				
		local randomPlayer = lobby[math.random(1, #lobby)]
		randomPlayer.TeamColor = game.Teams[teams[teamNumber]].TeamColor
		--toMap(randomPlayer)
		
		local spectatorNumber = 0
		for _, player in pairs(game.Players:GetChildren()) do
			if player.TeamColor == game.Teams["Lobby"].TeamColor then
				local character = player.Character
				if character and character:FindFirstChild("Torso") then
					spectatorNumber = spectatorNumber + 1
				end
			end
		end
		--print(teamNumber)
		--print(spectatorNumber)
		teamNumber = (teamNumber == 1) and 2 or 1
	until spectatorNumber == 0
end

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:wait()
	getTeams()
end)

For some reason every player joins red team. Can anyone see why? I don't want the code altered much. The code has to be in a function, as I call it later in the script.

6 comments

0

What do you mean?

0

this might sound dumb but have you tried using math.randomseed(tick())?

0

I think that would just completely random, I need the teams to be kept even. Completely random could still result in even teams.

0

but you could add checks to make sure teams are even

0

use game.Players:GetPlayers()) btw

1 answer

✓ Accepted answer

I'll just, uhh, rewrite the script for you. Follow my lead:

-- Save these constants so that you don't have to keep
-- doing game.Teams.TeamName all the time.
local Teams = game:GetService("Teams")
local RED_TEAM = Teams:WaitForChild("Red")
local BLUE_TEAM = Teams:WaitForChild("Blue")
local LOBBY_TEAM = Teams:WaitForChild("Lobby")

-- Variable for whether or not the game is currently in intermission
-- i.e. where or not players should be in the lobby.
local isIntermission = false

-- Assign a player to a team in a balanced manner.
local function AssignTeam(player)
	-- If we're in intermission mode, then assign that player to lobby.
	if isIntermission then
		player.Team = LOBBY_TEAM		
	else
		local redTeamPlayerCount = #RED_TEAM:GetPlayers()
		local blueTeamPlayerCount = #BLUE_TEAM:GetPlayers()
		-- if blues have fewer players, then
		-- assign them to team blue.
		if blueTeamPlayerCount < redTeamPlayerCount then
			player.Team = BLUE_TEAM
		-- same for red
		elseif redTeamPlayerCount < blueTeamPlayerCount then
			player.Team = RED_TEAM
		-- else if both teams are tied in player count,
		-- assign them to a random team.
		else
			local coinFlip = math.random()
			if coinFlip < 0.5 then
				player.Team = RED_TEAM
			else
				player.Team = BLUE_TEAM
			end
		end
	end
end

-- Automatically assign new players to a team.
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
	AssignTeam(player)
end)

-- Call this function if you want to begin a round
-- i.e. exit intermission
local function BeginRound()
	isIntermission = false
	for _, player in pairs(Players:GetPlayers()) do
		-- Wrap in a spawn() just in case a player won't get assigned for some reason.
		spawn( function()
			AssignTeam(player)
			-- Maybe respawn players while you're at it?
		end)
	end
end

-- Call this function if you want to exit the round.
-- i.e. enter intermission/lobby
local function EndRound()
	isIntermission = true
	for _, player in pairs(Players:GetPlayers()) do
		-- Wrap in a spawn() just in case a player won't get assigned for some reason.
		spawn( function()
			AssignTeam(player)
			-- Maybe respawn players while you're at it?
		end)
	end
end

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