How to add a Teams System to this Round System?

Hey guys, I’ve made a round system where everything works as planned, but I am just stuck on how to add a teams system to it where when the players are being teleported to the chosen map, the players are all split evenly into two teams- viking and samurai.

I’m stuck on how I would make this, and I would also like to know how to make it so that when all players from a team die the other team gets a win.
MY CODE:

local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local Maps = ServerStorage:WaitForChild('Maps'):GetChildren()
local Status = ReplicatedStorage:WaitForChild('Status')

while true do
	
	--Intermission
	
	local Countdown = 20 -- intermission, make this as long as you want
	
	repeat wait(1)
		Countdown = Countdown - 1
		
		Status.Value = 'Intermission : '..Countdown
	until Countdown <= 0
	
	--Choose the map.
	
	Status.Value = 'Choosing Map...'
	
	local ChosenMap = Maps[math.random(1, #Maps)]:Clone()
    wait(0.5)
	local Spawns = ChosenMap:FindFirstChild('Spawns'):GetChildren()

	wait(3) -- little pause, make this as long as you want
	
	ChosenMap.Parent = workspace
	Status.Value = 'Map chosen, teleporting players.'
	
	wait(2) -- little pause, make this as long as you want

	--teleport the players
	
	local playersInRound = {} -- track alive players
	local connections = {} -- store connections to disconnect later
	for _, Player in pairs(Players:GetChildren()) do
		if Player.Character and Player.Character:FindFirstChild('Humanoid') then
			local RandomSpawn = Spawns[math.random(1, #Spawns)]
			Player.Character.HumanoidRootPart.CFrame = RandomSpawn.CFrame

			table.insert(playersInRound, Player) -- add all players to table
			connections[Player.Name] = Player.Character.Humanoid.Died:Connect(function()
				table.remove(playersInRound, table.find(playersInRound, Player))
			end)
		end
	end
	
	connections["Removing"] = game.Players.PlayerRemoving:Connect(function(player) -- remove player from list if they leave
		local ind = table.find(playersInRound, player)
		if ind then
			table.remove(playersInRound, ind)
		end
	end)
	
	Countdown = 5 -- Starting Round In, make this as long as you want
	
	repeat wait(1)
		Countdown = Countdown - 1
		
		Status.Value = 'Starting Round in : '..Countdown
	until Countdown <= 0
	
	Countdown = 60 -- Game Time
	
	repeat wait(1)
		Countdown = Countdown - 1
		
		Status.Value = 'Ingame : '..Countdown
	until Countdown <= 0 or #playersInRound == 1

    -- Determine winner and give reward
    if #playersInRound == 1 then
        local success, message = pcall(function()
            local player = playersInRound[1]
            print("The Winner is: " .. player.Name)
            Status.Value = player.Name .. " won the game!"
            player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1
            player.leaderstats.Denarius.Value = player.leaderstats.Denarius.Value + 100
        end)
        if not success then
            warn("An error occurred while rewarding winner: " .. message)
        end
    else
        print("There was no single winner.")
        Status.Value = "There was no single winner this round."
    end

	--Kill the players
	for _, connection in pairs(connections) do -- disconnect connections to prevent memory leaks
		connection:Disconnect()
	end
	
	for _, Player in pairs(playersInRound)do
		Player:LoadCharacter()
	end
	
	ChosenMap:Destroy()
	
	Status.Value = 'Round Ended, waiting for new game.'
	
	wait(4) -- little pause, make this as long as you want.
	
end

Help is very appreciated. Thanks :slight_smile:

3 Likes

Hello.
As I see you doing similiar script like AlvinBlox did.
But I don’t know how you want include the team Map1 and Map2 when you don’t know what spawnpoint is where.

4 Likes

As I said, the round system works perfectly… I’m just not sure how to add a system which splits the plrs in two teams at the start of every round and to make it so that when every one in one team dies the other team wins.

If any of you could help me with this, that’d be awesome!

Essentially you would want to create two different tables that host the different teams. When you add the players to the game you will check to see which team has fewer players and add it to that one. If the teams have an even amount of players you will want to choose a random team or just choose any team. You will want to change the spawn locations to team-specific spawns and send the player to the CFrame accordingly. You will then want to update the died and removed connections to remove the player from the team tables that they are a part of. You will want to then change the if statement that checks if there is 1 player left and change that to see if either team has players (size of the table). If one of the teams won by the time the timer runs out then you will iterate over the winning team and reward each player. That is how I would do it at least.

2 Likes