Help with swordfighting round system

So I am trying to make this round system where if there are 2 players or more after the intermission time has passed every player is sent to a map in random spawns and compete with swords until one is standing, then it returns everyone into the lobby, but there are some problems I noticed when playtesting:

  • The UI text is acting odd, sometimes lagging or advancing too much;
  • When the players reset, the round still goes on;
  • If someone wins, the next round continues despite someone winning.

Help appreciated!

local players = game:GetService("Players")

local intermissionTime = 20
local roundTime = 60

local playersInRound = {}

local roundDisplay = game.ReplicatedStorage:WaitForChild("RoundDisplay")

local mapSpawns = workspace:WaitForChild("Spawns")

players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		char:WaitForChild("HumanoidRootPart").CFrame = workspace.SpawnLocation.CFrame + Vector3.new(0, 3, 0)
	end)
end)

local function enoughPlayers()
	return #players:GetPlayers() >= 2
end

local function updateDisplay(str)
	roundDisplay.Value = str
end

local function sortPlayers()
	playersInRound = {}
	for i, v in ipairs(players:GetPlayers()) do
		print("Sorting "..v.Name.."...")
		
		local char = v.Character or v.CharacterAdded:Wait()
		local humanoid = char:FindFirstChildWhichIsA("Humanoid")
		
		local playerSword = game.ReplicatedStorage.ClassicSword:Clone()
		local selectedSpawn = mapSpawns:GetChildren()[math.random(1, #mapSpawns:GetChildren())]
		
		if not table.find(playersInRound, v) then
			table.insert(playersInRound, v)
		end
		
		playerSword.Parent = v.Backpack
		char:WaitForChild("HumanoidRootPart").CFrame = selectedSpawn.CFrame + Vector3.new(0, 3, 0)
		
		humanoid.Died:Connect(function()
			table.remove(playersInRound, table.find(playersInRound, v))
			playerSword:Destroy()
		end)
	end
end

local function roundMechanic(it, rt)
	for i = it, 0, -1 do
		if not enoughPlayers() then
			break
		end
		updateDisplay("Round begins in "..i)
		task.wait(1)
	end
	sortPlayers()
	for i = rt, 0, -1 do
		if not enoughPlayers() then
			updateDisplay("Not enough players, restarting...")
			task.wait(3)
			for i, v in ipairs(playersInRound) do
				v.Backpack:WaitForChild("ClassicSword"):Destroy()
				v.Character:WaitForChild("HumanoidRootPart").CFrame = workspace.SpawnLocation.CFrame + Vector3.new(0, 3, 0)
				table.remove(playersInRound, table.find(playersInRound, v))
			end
			break
		end
		if #playersInRound == 1 then
			updateDisplay("Player "..playersInRound[1].Name.. " has won!")
			task.wait(3)
			for i, v in ipairs(playersInRound) do
				v.Backpack:WaitForChild("ClassicSword"):Destroy()
				v.Character:WaitForChild("HumanoidRootPart").CFrame = workspace.SpawnLocation.CFrame + Vector3.new(0, 3, 0)
				table.remove(playersInRound, table.find(playersInRound, v))
			end
			break
		end
		updateDisplay("Round ends in "..i)
		print(playersInRound)
		task.wait(1)
	end
end

while true do
	if not enoughPlayers() then
		updateDisplay("Not enough players")
		task.wait(0.5)
	else
		roundMechanic(intermissionTime, roundTime)
	end
end


You should probably clear the playersInRound table after the round is over.
Do

table.clear(playersInRound)

after everything is over

This was a bit helpful, but problems still persist

Bumping to see if anyone knows this.
I made some small changes, but I’m not sure if they do work. Help is still appreciated

local players = game:GetService("Players")

local intermissionTime = 20
local roundTime = 60

local playersInRound = {}

local roundDisplay = game.ReplicatedStorage:WaitForChild("RoundDisplay")

local mapSpawns = workspace:WaitForChild("Spawns")

players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		char:WaitForChild("HumanoidRootPart").CFrame = workspace.SpawnLocation.CFrame + Vector3.new(0, 3, 0)
	end)
end)

local function enoughPlayers()
	return #players:GetPlayers() >= 2
end

local function updateDisplay(str)
	roundDisplay.Value = str
end

local function sortPlayers()
	playersInRound = {}
	for i, v in ipairs(players:GetPlayers()) do
		print("Sorting "..v.Name.."...")
		
		local char = v.Character or v.CharacterAdded:Wait()
		local humanoid = char:FindFirstChildWhichIsA("Humanoid")
		
		local playerSword = game.ReplicatedStorage.ClassicSword:Clone()
		local selectedSpawn = mapSpawns:GetChildren()[math.random(1, #mapSpawns:GetChildren())]
		
		if not table.find(playersInRound, v) then
			table.insert(playersInRound, v)
		end
		
		playerSword.Parent = v.Backpack
		char:WaitForChild("HumanoidRootPart").CFrame = selectedSpawn.CFrame + Vector3.new(0, 3, 0)
		
		humanoid.Died:Connect(function()
			table.remove(playersInRound, table.find(playersInRound, v))
			playerSword:Destroy()
		end)
	end
end

local function roundMechanic(it, rt)
	for i = it, 0, -1 do
		if not enoughPlayers() then
			break
		end
		updateDisplay("Round begins in "..i)
		task.wait(1)
	end
	updateDisplay("Round starting...")
	task.wait(3)
	sortPlayers()
	for i = rt, 0, -1 do
		if not enoughPlayers() then
			updateDisplay("Not enough players, restarting...")
			task.wait(3)
			for i, v in ipairs(playersInRound) do
				v.Backpack:WaitForChild("ClassicSword"):Destroy()
				v.Character:WaitForChild("HumanoidRootPart").CFrame = workspace.SpawnLocation.CFrame + Vector3.new(0, 3, 0)
			end
			print("Game ended with "..#playersInRound.." ingame")
			table.clear(playersInRound)
			break
		end
		if #playersInRound == 1 then
			updateDisplay("Player "..playersInRound[1].Name.. " has won!")
			task.wait(3)
			for i, v in ipairs(playersInRound) do
				v.Backpack:WaitForChild("ClassicSword"):Destroy()
				v.Character:WaitForChild("HumanoidRootPart").CFrame = workspace.SpawnLocation.CFrame + Vector3.new(0, 3, 0)
			end
			print("Game ended with "..playersInRound[1].Name.." as the winner")
			table.clear(playersInRound)
			break
		end
		updateDisplay("Round ends in "..i)
		print("There are currently "..#playersInRound.." remaining")
		task.wait(1)
	end
	if #playersInRound > 1 then
		updateDisplay("No one has won, restarting")
		task.wait(3)
		for i, v in ipairs(playersInRound) do
			v.Backpack:WaitForChild("ClassicSword"):Destroy()
			v.Character:WaitForChild("HumanoidRootPart").CFrame = workspace.SpawnLocation.CFrame + Vector3.new(0, 3, 0)
		end
		print("Game ended with "..#playersInRound.." ingame")
		table.clear(playersInRound)
	end
end

while true do
	if not enoughPlayers() then
		updateDisplay("Not enough players")
		task.wait(0.5)
	else
		roundMechanic(intermissionTime, roundTime)
	end
end