Simple yet professional Timer Module

Introduction

This one timer object will most likely satisfy your needs.

Constructor

Timer.New(FormatFunction)

Parameter Type Description
FormatFunction function Optional. Custom formatting function
Return Type Description
timer object timer object inheriting all the methods from the module
local TimerObject = require(script.TimerObject)
local timer = TimerObject.new()

timer:Start(100)
timer.Updated:Connect(function()
    print(timer.Time.Format)
end)

FormatFunction

The FormatFunction gets passed the following arguments:

Argument Type Description
timer timer object
remainder number Time left to reach EndTime (in seconds)

Your function has to return a string which will then be seen in Timer.Time.Format.
If you don’t pass your own custom function, the function down below will be used.

local TimerObject = require(script.TimerObject)
local function timerFunction(timer, remainder)
	local function addZeros(s) return (s:len() == 1 and "0"..s) or s end
	Remainder = math.round(Remainder)
	Remainder = math.clamp(Remainder, 0, math.huge)
	local minutes = math.floor(Remainder/60)
	Remainder -= minutes*60

	return addZeros(tostring(minutes))..":"..addZeros(tostring(Remainder))
end

local timer = TimerObject.new(timerFunction)

timer:Start(100)
timer.Updated:Connect(function()
    print(timer.Time.Format)
end)

Properties

timer.Time

Index Type Description
Time.Seconds number Id of item to fetch
Time.Format string Formatted version of Seconds (FormatFunction)

Methods

timer:Start(StartTime, EndTime, Multiplier)

Starts the timer.

Parameter Type Description Default
StartTime number Required. The Time, the Timer will count down/up from No default
EndTime number Optional. The Time, the Timer will count down/up to 0
Multiplier number Optional. A Multiplier for the timer speed (x seconds timer per 1s) 1

StartTime and EndTime are in seconds

local TimerObject = require(script.TimerObject)
local timer = TimerObject.new()

timer:Start(100, 90)

timer:Stop()

Stops the timer. It can’t be resumed. To restart it, start it again.

timer:Pause()

Pauses the timer. It can be resumed by calling timer:Resume().

timer:Resume()

Resumes the timer. It continues where it left.

Events

timer.Updated

Fires whenever the timer.Time.Seconds changes

local TimerObject = require(script.TimerObject)
local timer = TimerObject.new()

timer:Start(100)
timer.Updated:Connect(function()
    print(timer.Time.Format)
end)

timer.Finished

Fires whenever the timer reaches its EndTime

local TimerObject = require(script.TimerObject)
local timer = TimerObject.new()

timer:Start(10)
timer.Finished:Connect(function()
    print("Timer finished!")
end)

Roblox model
View full documentation (on git)

Old Versions
Version 1.0.0

Introduction

I've seen multiple people asking for help about different types of timers in their games and therefore decided to create an object oriented module to (hopefully) end this search once and forever.

The timer object has many different properties and methods and I highly advise you to either edit the provied (uncopylocked) roblox place or to read throught the documentation on github (see below).

Quick Overview

Constructor Functions

Countdown.newSimple() ⇾ Countdown object

Parameter Description
Time number Starting Time to count down from

Countdown.newAdvanced() ⇾ Countdown object

Parameters Description
Time : number Starting Time to count down from
FormatFunction : function A custom format function
IsMillis : bool If true Then the preciser time function tick() will be used

Properties

.TimeRemaining

TimeRemaining keeps track of the time remaining untill the countdown is done. TimeRemaining stores that data in two different ways, TimeRemaining.format and TimeRemaining.unix

TimeRemaining.format

TimeRemaining.format stores the formatted string of time remaining : string

TimeRemaining.unix

TimeRemaining.unix stores the unformatted number of secconds remaining : number

Events

.Updated

Updated is a bindable event.Event that gets fired each time the remaining time of the object is updated.

.Finished

Finished is a bindable event.Event that gets fired whenever the countdown object finishes or is stopped.

Basic Usage Example

local Countdown = require(ServerStorage.ModuleScripts.Countdown)
local CountdownObject = Countdown.newSimple(80) -- Creates a countdown object with an 80 seconds timer.
local StringValue = workspace.StringValue
CountdownObject:Start() -- Starts counting down.

CountdownObject.Updated:Connect(function()
    StringValue.Value = CountdownObject.TimeRemaining.format -- Set StringValue's value to the formatted time remaining.
end)

Download or View

Roblox Model
Roblox Place (open source)
View full documentation

9 Likes

Maybe you weren’t the solution to that person problem, but you were mine

Keep updating and making awesome resources!

1 Like

I’m glad my modules solved your problem and helped you develop your own game! Keep up!

I found some errors myself and have fixed them in v2 now.