How to make a timer with minutes and seconds

So I have an idea on making a timer in which when the timer runs out, an entity spawns. But I don’t know where to start since I don’t know how to make a timer like this:
1:52
and counts down. I only know how to do the seconds version. Can someone help me?

2 Likes

here is an example using 105 seconds that comes out to 01:45 in time 1 min 45 seconds

local timeInSecondsHere = 105
local newTimeText = os.date('%M:%S', timeInSecondsHere)
print(newTimeText)

documentation of os date if you need to learn more about formatting time

1 Like

I did a script that looked like this. But the value was nil everytime.

script:

local textlabel = script.Parent

local function startTimer(timeinseconds)
	local newTimeText = os.date('%M:%S', timeinseconds)
	for i=1, timeinseconds do
		timeinseconds -= 1
		textlabel.Text = timeinseconds
		if timeinseconds == 0 then
			print("end")
			return
		end
	end
end

startTimer(120)

you need to have a wait in there somewhere to wait 1 second during each loop
and also the formated text needs updated each time the seconds change so you need to have that right before the textlabel update or at it

Here is an example using your base script there

local textlabel = script.Parent

local function startTimer(timeinseconds)

	for i=1, timeinseconds do
		timeinseconds -= 1
		textlabel.Text = os.date('%M:%S', timeinseconds)  -- move this here to display it to textlabel directly
		
		if timeinseconds == 0 then
			print("end")
			return
		end
		task.wait(1)   -- need to wait a second before looping again..
	end
end

startTimer(120)
1 Like

Creating a timer is really easy. The main issue is finding a way to replicate it to the clients. You could send a event every second (which not very network optimized) or send a event that tells the player to start a timer in the client side.

I’m gonna give you an example of script that sends a event every second.

function format_time(seconds)
	local minutes = math.floor(seconds / 60)
	local secs = seconds % 60
	return string.format("%d:%02d", minutes, secs)
end

local myEvent = game.ReplicatedStorage:WaitForChild("TimerEvent")
local breakTimer = false
local function startTimer(timeInSeconds: number)
	-- assigns the starting time
	local currentTime = timeInSeconds
	task.spawn(function()
		-- loop that runs every 1 second
		while task.wait(1) do
			if breakTimer then
				-- here you can do anything you want when the time's up
				print("Times up, oh no!")
				breakTimer = false
				break
			end
			-- here, you can notify the client that 1 second elapsed
			-- example
			local formattedTime = format_time(currentTime) -- ex: 3:22
			local players = game.Players:GetPlayers()
			for _, player in players do
				myEvent:FireAllClients(formattedTime)
			end

			-- reduce time by 1 second
			currentTime -= 1

			-- if time elapsed without interruption.
			if currentTime <= 0 then
				for _, player in players do
					myEvent:FireAllClients("Time's up")
				end
				breakTimer = true
			end
		end
	end)
end

local function endTimer()
	breakTimer = true
	myEvent:FireAllClients("Time's up")
end


-- example test code

-- start timer for 0:20
startTimer(20)

-- wait 5 seconds
task.wait(5)

-- end timer for any reason if needed, or just wait it to end.
endTimer()

You can place the code in your current script and use the startTimer function with the first argument being how long it will take.

After doing that, create a RemoteEvent in ReplicatedStorage called TimerEvent

Now, create a TextLabel under a screengui in the ui and design it as you want.

Then, under that label create a local script with the following code.

local event = game.ReplicatedStorage.TimerEvent

local timerText = script.Parent
event.OnClientEvent:Connect(function(newTime)
	timerText.Text = newTime
end)

If any errors arise or you need to adjust it, let me know! :slight_smile:

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