Timer Survivors

I need help to make it so when no one is in a table it will stop the “Ingame” timer and begin the “Preround” timer. Sorry for the bad formatting.

–Game Stats
local roundLength = 60 --Change this value to edit the ingame length
local preroundLength = 10 --Change this value to edit the preround length
local InRound = game.ReplicatedStorage:WaitForChild(“InRound”)
local LobbySpawn = game.Workspace.LobbySpawn
local GameSpawn = game.Workspace.GameSpawn
local Status = game.ReplicatedStorage:WaitForChild(“Status”)
local PLRwaitingList = {}
local PLRingame = {}

InRound.Changed:Connect(function()
if InRound.Value == true then
local UpperTilesClone = game.ReplicatedStorage.UpperTiles:Clone()
UpperTilesClone.Parent = game.Workspace
UpperTilesClone.Name = “UpperTiles”

	local BottomTilesClone = game.ReplicatedStorage.BottomTiles:Clone()
	BottomTilesClone.Parent = game.Workspace
	BottomTilesClone.Name = "BottomTiles"

	game.Workspace.GameSpawn.Bell.Playing = true
	
	game.Workspace.GameSpawn.ThemeMusic.Playing = true

	wait(1)

	PLRingame = PLRwaitingList

	for _, Player in pairs(PLRingame) do
		local Character = Player.Character
		if Character then
			local HumanoidRootPart = Character.HumanoidRootPart
			if HumanoidRootPart then
				HumanoidRootPart.CFrame = GameSpawn.CFrame
			end
		end
	end
else
	for _, player in pairs(PLRingame) do
		local Character = player.Character
		if Character then
			local HumanoidRootPart = Character.HumanoidRootPart
			if HumanoidRootPart then
				HumanoidRootPart.CFrame = LobbySpawn.CFrame
	PLRingame = {}
	game.Workspace.UpperTiles:Destroy()
	game.Workspace.BottomTiles:Destroy()
			end
		end
	end
	end
		end)

local function roundTimer()
while wait() do
for i = preroundLength, 1, -1 do
local Click = game.Workspace.GameSpawn.TimerClick
Click:Play()
InRound.Value = false
wait(1)
Status.Value = “Preround: “… i …” seconds left!”
end
for i = roundLength, 1, -1 do
InRound.Value = true
wait(1)
Status.Value = “Game: “… i …” seconds left!”
end
end
end

spawn(roundTimer)

game.ReplicatedStorage.WaitingOut.OnServerEvent:Connect(function(PLRpressed)
local WaitingOutButtonPress = PLRpressed
print(WaitingOutButtonPress)
for Index, Player in pairs(PLRwaitingList) do
if Player == PLRpressed then
table.remove(PLRwaitingList, Index)
end
end
print(PLRwaitingList)
end)
game.ReplicatedStorage.WaitingForAGame.OnServerEvent:Connect(function(PLRpressed)
local AwaitingGameButtonPress = PLRpressed
print(AwaitingGameButtonPress)
table.insert(PLRwaitingList, AwaitingGameButtonPress)
print(PLRwaitingList)
end)

Any help is appreciated!

The formatting made this really confusing although I would highly recommend having a spawn(func() for a function which will constantly check this “PLRinGame” table for each second passed in the game or simply while true do wait(1). The check would simply be getting the length of the array via how many player instances/ids are in the table. And if it returns 0 then you want to essentially have a global variable which you can set to true. By setting this to true this will cause your functions on the different threads to stop running as it essentially is saying “theirs no more players so continuing isn’t needed”.