How can I stop the timer when no players are active?

Hello devs!
So, I’m making a story game but the problem is that if I press play and then go back again the timer keeps on going. Is there a way to stop the timer when no players are active?
Here are the scripts;

  • Output
local Round = require(script.Round)
local Player = require(script.Player)
local RoundInfo = game.ReplicatedStorage:WaitForChild("RoundInfo")
local activePlayers = RoundInfo.Players

while wait(1) do
	Player.RespawnAllPlayers()
	activePlayers:ClearAllChildren()
	
	repeat
		wait(1)
	until #activePlayers:GetChildren() > 0
	
	Round.Countdown("Intermission", 10)
	
	Player.TeleportPlayers(workspace.Spawn)
	Round.Countdown("Round", 8)
end
  • Round Module
local round = {}

local function toMS(s)
	return ("%02i:%02i"):format(s/60%60, s%60)
end

function round.Countdown(status, length)
	local RoundInfo = game.ReplicatedStorage:WaitForChild("RoundInfo")
	RoundInfo.Status.Value = status
	RoundInfo.Timer.Value = length
	
	for i = length, 0, -1 do
		RoundInfo.Timer.Value = toMS(i)
		wait(1)
	end
end

return round
  • Player Module
local player = {}
local RoundInfo = game.ReplicatedStorage:WaitForChild("RoundInfo")
local Events = game.ReplicatedStorage:WaitForChild("Events")
local ToggleMenuRE = Events:WaitForChild("ToggleMenuRE")
local IsActiveRE = Events:WaitForChild("IsActiveRE")
local NotActiveRE = Events:WaitForChild("NotActiveRE")

function player.TeleportPlayer(playerName, tpLocation)
	local activePlayer = game.Players:FindFirstChild(playerName)
	
	if activePlayer then
		local char = activePlayer.Character or activePlayer.CharacterAdded:Wait()
		
		if char:FindFirstChild("HumanoidRootPart") then
			char.HumanoidRootPart.CFrame = tpLocation.CFrame
			ToggleMenuRE:FireClient(activePlayer, false)
		end
	end
end

function player.TeleportPlayers(tpLocation)
	for i, plr in pairs(RoundInfo.Players:GetChildren()) do
		player.TeleportPlayer(plr.Name, tpLocation)
	end
end

function player.RespawnAllPlayers()
	for i, plr in pairs(RoundInfo.Players:GetChildren()) do
		local activePlayer = game.Players:FindFirstChild(plr.Name)
		
		if activePlayer then
			activePlayer:LoadCharacter()
			ToggleMenuRE:FireClient(activePlayer, true)
		end
	end
end

game.Players.PlayerAdded:Connect(function(plr)
	ToggleMenuRE:FireClient(plr, true)
end)

IsActiveRE.OnServerEvent:Connect(function(plr)
	local tag = Instance.new("StringValue", RoundInfo.Players)
	tag.Name = plr.Name
end)

NotActiveRE.OnServerEvent:Connect(function(plr)
	local tag = RoundInfo.Players:FindFirstChild(plr.Name)
	
	if tag then
		tag:Destroy()
	end
end)

return player
  • The LocalScript where I press the play and back button
PlayButton.MouseButton1Click:Connect(function()
    IsActiveRE:FireServer()
end)

BackButton.MouseButton1Click:Connect(function()
    NotActiveRE:FireServer()
end)

Sorry if this is really big.

N e v e r m i n d
I found a post on how to pause a loop. Here