How can i do a timer?

  1. What do you want to achieve?
    Hello. I need to make a timer for my game.
    Something like that :
    [ 00:00:00 ]
    M : S : MS

  2. What is the issue?
    I don’t know how to do it.

  3. What solutions have you tried so far?
    I looked on forums and i didn’t find anything.

4 Likes

I managed to script the timer myself

local label = script.Parent

local function startTimer(initialTime)
	for i = initialTime, 0, -1 do
		label.Text = i
		task.wait(1)
	end
end

startTimer(10)

You found a solution but here’s a script for those who weren’t sure.

Here’s the model file.
repro.rbxm (3.1 KB)

And if you instead meant a timer that displays the length of time the player has been in the server (which counts upwards from zero), then here you go.

local label = script.Parent
local currentTime = 0

local function formatTime()
	local hours = math.floor(currentTime / 3600)
	local minutes = math.floor((currentTime - (hours * 3600))/60)
	local seconds = (currentTime - (hours * 3600) - (minutes * 60))
	local format = "%02d:%02d:%02d"
	return format:format(hours, minutes, seconds)
end

local function startTimer()
	while true do
		currentTime += 1
		label.Text = formatTime()
		task.wait(1)
	end
end

startTimer()

Model file below for reproduction purposes.

repro.rbxm (3.3 KB)

2 Likes

Hello, my script that i tried dosen’t work but does this script goes down or up ?

What type of timer are you trying to achieve? A shared timer? One which goes up or one which goes down? Please provide as many details as you can.

Im sorry, that i don’t give too much details i have difficulties with that.

I need a timer that goes down on the server everyone can see it and i need it to count minutes seconds and milliseconds

Oh and the timer is on a surfacegui if this count as a detail

And i saw a tutorial on youtube that was what i wanted but it dosen’t work


local TIMER_BINDING_NAME = "Timer"

local Label = script.Parent

local function startTimer(timeLeft)
	local delayEvent = Instance.new("BindableEvent")
	
	RunService:BindToRenderStep(TIMER_BINDING_NAME, Enum.RenderPriority.Last.Value - 1, function(delta)
		timeLeft -= delta
		
		local minutes = math.floor(timeLeft / 60)
		local seconds = math.floor(timeLeft % 60)
		local hundreths = math.floor(timeLeft % 1 * 100)
		
		Label.Text = string.format("%02i:%02i:%02i", minutes, seconds, hundreths)
		
		if timeLeft <= 0 then
			--delayEvent:Fire()
			RunService:UnbindFromRenderStep(TIMER_BINDING_NAME)
		end
	end)
	
	delayEvent.Event:Wait()
	
end

startTimer(50)

game.ReplicatedStorage.Events.Server.Countdown.Event:Connect(function(timer)
	startTimer(timer)
end)```
local label = script.Parent

local function formatTime(currentTime)
	local hours = math.floor(currentTime / 3600)
	local minutes = math.floor((currentTime - (hours * 3600))/60)
	local seconds = (currentTime - (hours * 3600) - (minutes * 60))
	local format = "%02d:%02d:%02d"
	return format:format(hours, minutes, seconds)
end

local function startTimer(currentTime)
	while true do
		currentTime -= 1
		label.Text = formatTime(currentTime)
		task.wait(1)
	end
end

startTimer(5000) --Change this to specify where the timer should start from.

Here’s a server script which works with a SurfaceGui instance.

repro.rbxm (5.8 KB)

Above is the model file.

Here’s an image so you can see how it works.

image

1 Like

Yes, this is what i want but how i can make it countdown

  • Minutes : Seconds: Miliseconds
local run = game:GetService("RunService")
local label = script.Parent

local function formatTime(currentTime)
	local hours = math.floor(currentTime / 3600)
	local minutes = math.floor((currentTime - (hours * 3600))/60)
	local seconds = math.floor((currentTime - (hours * 3600) - (minutes * 60)))
	local milliseconds = 1000 * (currentTime - math.floor(currentTime))
	local format = "%02d:%02d:%02d:%03d"
	return format:format(hours, minutes, seconds, milliseconds)
end

local function startTimer(currentTime)
	run.Stepped:Connect(function(elapsed, step)
		currentTime -= step
		label.Text = formatTime(currentTime)
	end)
end

startTimer(5000)

Hours:Minutes:Seconds:Milliseconds format.

repro.rbxm (5.8 KB)

The milliseconds change very quickly.

image

1 Like

Thank you !
Would you know how i can do to play a sound when a second pass ?

local run = game:GetService("RunService")
local sound = script.Sound --Put a sound instance inside the script named "Sound".
local label = script.Parent
local oldElapsed = 0

local function formatTime(currentTime)
	local hours = math.floor(currentTime / 3600)
	local minutes = math.floor((currentTime - (hours * 3600))/60)
	local seconds = math.floor((currentTime - (hours * 3600) - (minutes * 60)))
	local milliseconds = 1000 * (currentTime - math.floor(currentTime))
	local format = "%02d:%02d:%02d:%03d"
	return format:format(hours, minutes, seconds, milliseconds)
end

local function startTimer(currentTime)
	run.Stepped:Connect(function(newElapsed, step)
		if oldElapsed ~= newElapsed then
			sound:Play()
		end
		oldElapsed = newElapsed
		currentTime -= step
		label.Text = formatTime(currentTime)
	end)
end

startTimer(5000)

I’ve added an instruction as a comment to the script.

Ok, thank you a lot for your help and one last thing, how do i stop it when it’s at 0

local run = game:GetService("RunService")
local sound = script.Sound --Put a sound instance inside the script named "Sound".
local label = script.Parent
local oldElapsed = 0

local function formatTime(currentTime)
	local hours = math.floor(currentTime / 3600)
	local minutes = math.floor((currentTime - (hours * 3600))/60)
	local seconds = math.floor((currentTime - (hours * 3600) - (minutes * 60)))
	local milliseconds = 1000 * (currentTime - math.floor(currentTime))
	local format = "%02d:%02d:%02d:%03d"
	return format:format(hours, minutes, seconds, milliseconds)
end

local function startTimer(currentTime)
	run.Stepped:Connect(function(newElapsed, step)
		if oldElapsed ~= newElapsed then
			sound:Play()
		end
		oldElapsed = newElapsed
		currentTime -= step
		if currentTime <= 0 then
			label.Text = "Timer has finished!"
			return
		end
		label.Text = formatTime(currentTime)
	end)
end

startTimer(5)

image

There is an issue, the sound don’t play correctly it play then cut immediatly

Probably because the length of your sound is more than one second and it’s attempting to play the sound each second, I’d recommend just removing that part of the script.

1 Like

No the sound is less than a second

I wouldn’t recommend playing a sound each second anyway, it’d just get annoying after a while.

1 Like