How to reset countdown after its finished

I want the countdown to be able to start second time

When countdown finish first time and then i try to start it second time by entering the zone again it doesnt start

local part = script.Parent.Parent 
local playersTouching = {}
local maxPlayers = 4 
local touchEndDelay = 0.5
local countdownDuration = 10
local countdownCoroutine
local Event = game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("StartRound")
local Zone = require(game:GetService("ServerScriptService").Zone)
local container = script.Parent.Parent
local zone = Zone.new(container)
local Characters = {}



local function updateBillboard()
	local countText = #playersTouching .. "/" .. maxPlayers
	part.billboardPart.BillboardGui.Frame.players.Text = countText
end



local countdownStarted = false



local function startCountdown()
	if not countdownStarted then 
		countdownStarted = true 
		print("Countdown started.")
		countdownCoroutine = coroutine.create(function()
			for i = countdownDuration, 0, -1 do
				print("Countdown: " .. i)
				part.billboardPart.BillboardGui.Frame.time.Text = tostring(i) 
				wait(1)
			end

			if #playersTouching >= 1 then 
				
				startCountdown()
				
			else
				
				for _, player in pairs(playersTouching) do
					if player.Character then
						table.insert(Characters, player.Character)
					end
				end
				
				Event:Fire(playersTouching, Characters)
				countdownStarted = false 
				
			end
		end)
		coroutine.resume(countdownCoroutine)
	end
end




local function stopCountdown()
	
	if countdownCoroutine then
		print("Countdown stopped.")
		coroutine.yield(countdownCoroutine) 
		part.billboardPart.BillboardGui.Frame.time.Text = "0" 
		countdownCoroutine = nil
		countdownStarted = false
		if #playersTouching >= 2 then
			
			startCountdown()
			
		end
	end
	
end




local function playerTouching(player)
	
	for _, v in ipairs(playersTouching) do
		
		if v == player then
			
			return true
			
		end
	end
	return false
end




local function addPlayer(player)
	
	if not playerTouching(player) then
		table.insert(playersTouching, player)
		print(player.Name .. " started touching the part.")
		print("Total players touching: " .. #playersTouching)
		updateBillboard()

		if #playersTouching >= 1 and not countdownCoroutine then
			startCountdown()
		end
		
	end
end



local function removePlayer(player)
	
	for i, v in ipairs(playersTouching) do
		
		if v == player then
			
			table.remove(playersTouching, i)
			print(player.Name .. " stopped touching the part.")
			print("Total players touching: " .. #playersTouching)
			updateBillboard()
			
			if #playersTouching < 2 then
				
				stopCountdown()
				
			end
			
			break
		end
	end
end




zone.playerEntered:Connect(function(player)
	if player and #playersTouching < maxPlayers then
		addPlayer(player)
	end
end)


zone.playerExited:Connect(function(player)
	if player then
		if playerTouching(player) then
			removePlayer(player)
		end
	end
end)

Instead of creating a Countdown function, try making an infinite loop, and move this line: local countdownDuration = 10 inside the loop, and not outside.
I also don’t think there is a need for coroutine.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.