How to make a round timer?

Hey! I’m making a game that involves clicking buttons, then a door will open, then you can escape the building, however, I don’t know how long each round is because it depends on how long it takes the players to find and click all the buttons.

I was thinking of implementing some kind of set time, for example, 3 mins, and if they don’t complete in the 3 mins, everyone fails. However, if they do escape in the 3 mins, then an intermission would start blah blah blah.

So I’m thinking of setting a set time. But how?

Here is my main script, it’s kinda long.

SERVER SCRIPT

--// Statuses

local EndGameStatus = "All of the buttons have been activated! Get to the exit!"
local StartGameStatus  = "Come on, comrades! We can escape this."


game.ReplicatedStorage.Values.ActivatedButtons:GetPropertyChangedSignal("Value"):Connect(function()	
	-- Setting Value Values
	local buttonsLeft = 4 - game.ReplicatedStorage.Values.ActivatedButtons.Value
	print(buttonsLeft)
	game.ReplicatedStorage.Values.ButtonsLeft.Value = buttonsLeft
	
	local DuringGameStatus = "There are "..4 - game.ReplicatedStorage.Values.ActivatedButtons.Value .." buttons left!"
	
	game.ReplicatedStorage.Values.GameStatus.Value = DuringGameStatus
	
	if buttonsLeft == 0 then
		game.ReplicatedStorage.Values.GameStatus.Value = EndGameStatus
		
		wait(5)
		game.ReplicatedStorage.Values.GameStatus.Value = "30"
		wait(1)
		game.ReplicatedStorage.Values.GameStatus.Value = "29"
		wait(1)
		game.ReplicatedStorage.Values.GameStatus.Value = "28"
		wait(1)
		game.ReplicatedStorage.Values.GameStatus.Value = "27"
		wait(1)
		game.ReplicatedStorage.Values.GameStatus.Value = "25"
		wait(1)
		game.ReplicatedStorage.Values.GameStatus.Value = "24"
		wait(1)
		game.ReplicatedStorage.Values.GameStatus.Value = "23"
		wait(1)
		game.ReplicatedStorage.Values.GameStatus.Value = "22"
		wait(1)
		game.ReplicatedStorage.Values.GameStatus.Value = "21"
		wait(1)
		game.ReplicatedStorage.Values.GameStatus.Value = "20"
		wait(1)
		game.ReplicatedStorage.Values.GameStatus.Value = "19"
		wait(1)
		game.ReplicatedStorage.Values.GameStatus.Value = "18"
		wait(1)
		game.ReplicatedStorage.Values.GameStatus.Value = "17"
		wait(1)
		game.ReplicatedStorage.Values.GameStatus.Value = "16"
		wait(1)
		game.ReplicatedStorage.Values.GameStatus.Value = "15"
		wait(1)
		game.ReplicatedStorage.Values.GameStatus.Value = "14"
		wait(1)
		game.ReplicatedStorage.Values.GameStatus.Value = "13"
		wait(1)
		game.ReplicatedStorage.Values.GameStatus.Value = "12"
		wait(1)
		game.ReplicatedStorage.Values.GameStatus.Value = "11"
		wait(1)
		game.ReplicatedStorage.Values.GameStatus.Value = "10"
		wait(1)
		game.ReplicatedStorage.Values.GameStatus.Value = "9"
		wait(1)
		game.ReplicatedStorage.Values.GameStatus.Value = "8"
		wait(1)
		game.ReplicatedStorage.Values.GameStatus.Value = "7"
		wait(1)
		game.ReplicatedStorage.Values.GameStatus.Value = "6"
		wait(1)
		game.ReplicatedStorage.Values.GameStatus.Value = "5"
		wait(1)
		game.ReplicatedStorage.Values.GameStatus.Value = "4"
		wait(1)
		game.ReplicatedStorage.Values.GameStatus.Value = "3"
		wait(1)
		game.ReplicatedStorage.Values.GameStatus.Value = "2"
		wait(1)
		game.ReplicatedStorage.Values.GameStatus.Value = "1"
		wait(1)
		game.ReplicatedStorage.Values.GameStatus.Value = "Game Ended"
		wait(0.5)
		game.ReplicatedStorage.Events.EndGame:FireAllClients()
		game.ReplicatedStorage.Values.InMatch.Value = false
	end
end)

game.ReplicatedStorage.Events.RestartGame.OnServerEvent:Connect(function()
	game.ReplicatedStorage.Values.GameStatus.Value = "Intermission"
end)

Thank you.

2 Likes

You can try using loops and use a number value instead like this

local x=game.ReplicatedStorage.Values.GameStatus
local countdownTime=“make it a number Not a string”
i=coundownTime
while i>-1 do
x.Value=i
wait(1)
if(i==0) then
x.Value=“game ended“
end
i=i-1
end

Where would I put this part of the script?

1 Like

This is how I would do it:

(Of course you can remove hours or minutes).

local Time = script.Parent.TimeFrame.Uptime

local CountDownToTime = 3

local TimerActive = false

while true do 
	
	wait(1)
	
	TimerActive = not TimerActive
	
	if TimerActive then
			
		CountDownToTime = CountDownToTime - 1 
			
		local Seconds = CountDownToTime % 60 
		local Minutes = math.floor(CountDownToTime / 60) % 60 
		local Hours = math.floor(CountDownToTime / (60 * 60))
			
		if string.len(Seconds) < 2 then Seconds = "0" .. Seconds end 
		if string.len(Minutes) < 2 then Minutes = "0" .. Minutes end
		if string.len(Hours) < 2 then Hours = "0" .. Hours end
			
		local Format = Hours .. ":" .. Minutes .. ":" .. Seconds
			
		Time.Text = Format
		
		if CountDownToTime == 0 then 
			return
			-- Do things.
		end
	end
end

As someone that doesn’t know much about scripting, that confuses me.

Basing of what I can see, it seems as if this goes in the status bar, because of “Time.Text

This is how I put it,
https://gyazo.com/c343fbfa9cea2bdb9d093fbfea5fe4ff

You can do whatever you want with the UI, however, you have to make sure that the Time variable is the TextLabel that you use to show the countdown.

Is this a server uptime counter? I’m trying to make a round/match system with a countdown till how long’s left.

It was, however I just changed the script to be a countdown timer.
(Don’t mind the names of the UI and the script, sorry if it confused you).

1 Like

If you only want to make a second countdown timer this is how you would do the script:

local Time = script.Parent.TimeFrame.Uptime

local CountDownToTime = 10

local TimerActive = false

while true do 
	
	wait(1)
	
	TimerActive = not TimerActive
	
	if TimerActive then
			
		CountDownToTime = CountDownToTime - 1 
			
		local Seconds = CountDownToTime % 60 
			
		if string.len(Seconds) < 2 then Seconds = "0" .. Seconds end 
			
		local Format = Seconds
			
		Time.Text = Format
		
		if CountDownToTime == 0 then 
			return
			-- Do code.
		end
	end
end

This is a simple countdown timer with a game time of 180 seconds and a break of 30 seconds

local InGame = 0
local Seconds = 30

while true do
	wait(1)
	
	if Seconds == 0 and InGame == 0 then
	   InGame = 1
	   Seconds = 180
	   print("Start Game")
	end
		
	if Seconds == 0 and InGame == 1 then
	   InGame = 0
	   Seconds = 20
	   print("Game Over")
	end
	
	Seconds = Seconds - 1
	print(Seconds)
	
end
1 Like