Randomizing Team with Round system

Hi, i tried to make a brickbattle game with a round system in 2017-2018, inspired by Doomspire. I have only just came back to the game today, and wanted to add in a randomizing team system which would change teams every 5 minutes (length of round). Successfully i accomplished making a map system that switches every 5 minutes and alerts the users at the top when the round is over and when the map will be switched.

The script i have so far is below, and i was wondering how to alter it to swap teams every round. I have no clue how to.

I’m really quite a novice scripter so im sorry if none of this makes sense :L

wait(10) 

maps = {} 

Inform=Instance.new("Message",Workspace) 

blank="" 

RoundTime = 300



for i,v in pairs (game.ReplicatedStorage.Maps:GetChildren()) do

if v:IsA'Model' then

table.insert(maps, v.Name)

end

end

while true do

randomMap = maps[math.random(1,#maps)]

SelectedMap=game.ReplicatedStorage.Maps:WaitForChild(randomMap):Clone()

SelectedMap.Parent=game.Workspace

Inform.Text="Map chosen: "..SelectedMap.Name

SelectedMap:MakeJoints()

wait(3)

Inform.Text=blank

time = 300

local m = Instance.new("Hint")

m.Parent = game.Workspace

while time > 0 do

m.Text = time.. " seconds until this round is over! The current map is "..SelectedMap.Name.."!"



time = time -1

wait(1)

skeiohf = game.Workspace:FindFirstChild("Meteor")

if time < 1 then

SelectedMap:remove()

if skeiohf == true then

skeiohf:remove()

end

end

end

end

I’m not sure if that game is the same with this one:
https://www.roblox.com/games/1215581239/Doomspire-Brickbattle?refPageId=508198e7-3f81-4da3-9bee-04c1e379000
But this one is open sourced so you can check out how they randomize the teams.

Just click the “Edit” button
image

Edit:
The game control script under server script service has this function used for randomizing the teams:

script:WaitForChild("ShuffleTeams").OnInvoke = function()
	local teams = AssignableTeams()
	local numberOfTeams = #teams
	local playersList = game.Players:GetPlayers()
	local numberOfPlayers = #playersList

	-- Quit early if there are no teams, this avoids a divide by zero
	if #teams == 0 or #playersList == 0 then
		return
	end

	local playersPerTeam = math.floor(numberOfPlayers / numberOfTeams)
	local leftOverPlayers = numberOfPlayers - (playersPerTeam * numberOfTeams)
	local teamCount = {}

	for _, team in ipairs(teams) do
		teamCount[team] = 0
		for i = 1, playersPerTeam do
			local k = math.random(1, #playersList)
			local player = playersList[k]
			if player then
				player.TeamColor = team.TeamColor
				teamCount[team] = teamCount[team] + 1
				table.remove(playersList, k)
			end
		end
	end

	-- Now place the remaining players
	for i = 1, leftOverPlayers do
		local k, player = next(playersList)
		local teamIndex = math.random(1, #teams)
		local team = teams[teamIndex]
		if player and teamIndex then
			player.TeamColor = team.TeamColor
			table.remove(playersList, k)
			table.remove(teams, teamIndex)
		end
	end
end

Just to explain what’s going on here

local teams = AssignableTeams() --table of available teams in-game
local numberOfTeams = #teams --number of teams
local playersList = game.Players:GetPlayers() --table of current players
local numberOfPlayers = #playersList --number of current players
if #teams == 0 or #playersList == 0 then --this checks if the number of teams or players is equal to 0
	return --so it stops the function to save resources or end the game
end
local playersPerTeam = math.floor(numberOfPlayers / numberOfTeams) --computes for the number of players per team
local leftOverPlayers = numberOfPlayers - (playersPerTeam * numberOfTeams) --determines the number of players to be left over due to an un-even count
local teamCount = {} --this will hold the number of players per team

for _, team in ipairs(teams) do --this loops through the available teams
	teamCount[team] = 0 --this creates the team slots in the teamCount table
	for i = 1, playersPerTeam do --loops the number of players to be placed per team
		local k = math.random(1, #playersList) --picks a random number
		local player = playersList[k] --gets a random player
		if player then --checks if the player exists
			player.TeamColor = team.TeamColor --sets the player's team color
			teamCount[team] = teamCount[team] + 1 --sets the new player count on the current team
			table.remove(playersList, k) --removes the selected player from the players list to prevent re-shuffling
		end
	end
end
for i = 1, leftOverPlayers do --loops the number of left over players
	local k, player = next(playersList) --gets the index and player object from the left over players
	local teamIndex = math.random(1, #teams) -- gets a random number
	local team = teams[teamIndex] --gets a random team using random number
	if player and teamIndex then -- checks if the player and team exists
		player.TeamColor = team.TeamColor --sets player's team color
		table.remove(playersList, k) --removes player from player list to prevent re-shuffling
		table.remove(teams, teamIndex) --removes team to prevent unbalanced teams
	end
end
3 Likes

Thank you, appreciate it. I will make sure to try and interpret that. :slight_smile: Also genuinely appreciate that you tagged everything. Thanks so much.