Timer Module - The module for all your timer-related needs

Hi! I am releasing a timer module I made! I’ve seen some timer modules before so I added some features that will hopefully make it unique! This is my first post in #resources:community-resources so feel free to leave some constructive critisism! :happy3:

Source Code

Here is the source code for the module:

local funcs = {

}

local finished = false
local Time = 0
local Speed = 1
local formatcol = false
local stop = false
local status = "Idle"
local paused = false
local Target = math.huge
local CountUp = false
local tickSound = nil
local Visible = false

function findseconds()
	local seconds = math.fmod(Time,60)
	if string.len(seconds) == 1 then
		seconds = "0".. seconds
	end

	return seconds
end

function findminutes()
	local minutes = math.floor(Time/60)
	if string.len(minutes) == 1 then
		minutes = "0".. minutes
	end
	if tonumber(minutes) < 0 then
		minutes = 00
	end
	return minutes

end

function funcs.start(TIME,speed,colonformat,countup,target)
	if TIME == nil then
		TIME = Time
		speed = Speed
		colonformat = formatcol
		countup = CountUp
		target = Target
	end
	print("Started")
	status = "Started"
	Speed = speed
	
	formatcol = colonformat
	Target = target
	print(Target)
	Time = TIME
	if not speed then
		speed = 1
	end
	if colonformat == nil then
		colonformat = true
	end
	if countup == nil then
		countup = true
		CountUp = countup
	else
		if target == nil then
			warn("No target number specified; Timer will go on infinitely")
			target = math.huge
		end
		CountUp = countup
	end
	if not target then
		target = 0
	end
	
	Target = target
	Speed = speed
	formatcol = colonformat
	Time = TIME
	coroutine.wrap(function() 
		repeat 
			if Time  > 0 then
				if CountUp == true then
					Time = Time +1
				else
					Time = Time -1
				end
				
				if colonformat == true then
					local minutes = findminutes()
					local seconds = findseconds()
					script.Parent.Text = minutes..":".. seconds
					if tickSound ~= nil then
						print("ticksound isnt nil")
						local sound = Instance.new("Sound")
						sound.Parent = workspace
		
			
					sound.SoundId = "rbxassetid://".. tickSound
						sound:Play()
						sound.Ended:Connect(function()
							sound:Destroy()
						end)
						end
					else
					
					script.Parent.Text = Time
					if tickSound ~= nil then
						local sound = Instance.new("Sound")
						sound.Parent = script


						sound.SoundId = "rbxassetid://".. tickSound
						sound:Play()
						sound.Ended:Connect(function()
							sound:Destroy()
						end)
					end
				end
			else
				script.Parent:SetAttribute("Finished",true)
			end
			wait(Speed)
		until script.Parent:GetAttribute("Finished") == true or paused == true or Time >= Target
	end)()
end

function funcs.pause()
	status = "Paused"
	paused = true
end

function funcs.resume()
	if Time ~= 0 then
		status = "Started"
		script.Parent:SetAttribute("Finished",false)
		paused = false
	funcs.start(Time,Speed,formatcol)
	end
end

function funcs.stop()
	status = "Idle"
	script.Parent:SetAttribute("Finished",true)
	Speed = nil
	formatcol = nil
	Time = 0
	Target = math.huge
	local CountUp = false
	local tickSound = nil
end

function funcs.getStatus()
	return status
end

function funcs.changeSpeed(SPEED)
	if not SPEED then
		warn("Speed arguement missing")
	else
		Speed = SPEED
	end
end

function funcs.setTarget(TARGET)
	Target = TARGET
end

function funcs.changeCountUp(bool)
	CountUp = bool
end

function funcs.setParams(timerTime,speed,formatc,countup,target)
	if timerTime then
		Time = timerTime
	end
	if speed then
		Speed = speed
	end
	if formatc then
		formatcol = formatc
	end
	if countup then
		CountUp = countup
	end
	if target then
		Target = target
		end
end

function funcs.setTickSound(id)
	if id then
		tickSound = id
		end
end

function funcs.setTimerVisible(bool)
	Visible = bool
	script.Parent.Visible = bool
		
	end

return funcs
Model

Here is the link to the module

How to use

To use this module, in your script you must type:

local timer = require() --Put the path to the module inside the brackets
-- The variable can be named anything

Here is how to use all of the module’s features:

start(time,speed,colonformat,countup,target)

This function will start the timer.
time: The timer duration
speed: The timer interval duration
colonformat: When set to true the timer text will look like this: 0:00. When set to false the timer text will look like this: 0.
countup: When set to true, the timer will count up instead of counting down.
target: This is the number that the timer will stop at.
Not all of the parameters have to be used. The only compulsory parameter is time

pause()

This function will pause the timer.

stop()

This function will stop and clear the current timer.

getstatus()

This function will return the timer’s current status

changeSpeed(speed)

This function will change the timer’s interval duration without you having to start the timer again.

setTarget(target)

This function will change the target of the timer without you having to start the timer again.

changeCountUp(bool)

This will change whether the timer counts up or down without you having to start the timer again.

setParams(time,speed,formatc,countup,target)

This will set the parameters of the timer without starting it.

setTickSound(id)

This function will set the tick sound of the timer

setTimerVisible(bool)

This will determine whether there will be a visible timer or not

setTimerUI(uiElement)

This function will set the UI for the timer. This MUST have a text property

getTime()

This function will return the time on the timer.

Thanks for reading! :happy2:

15 Likes

Please tell me what you think of the module in the replies!

2 Likes

dang, i like that its more wel fitted for teh new people

im not a scripter so i dont kno efficient ways or like better ways

2 Likes

hi, i’m getting an error in line 81
Workspace.TimerModule:79: Workspace.TimerModule:81: attempt to compare number < table

2 Likes

Could you please show me the script where the module was used?

1 Like

Hey, so I’m trying to use it, but when I use getTime() function, it doesn’t appear to be a valid function in the module, and saying I attempted to call a nil value. In the Module itself, there is no getTime() defined it seems.

1 Like

My apologies. I’ll update the module soon

2 Likes

i deleted the script, but can u make a tutorial video? im confused

Suggestion, slightly modify this to be object oriented. That way I can make as many timers as I want in my game, and manage them all individually.

1 Like

This is really nice and saves a lot of time, Although it’d be even nicer to be able to manage multiple timers using it :eyes:

1 Like

Do let me know when you can update it, since was hoping to use this for my game.

lol what update are you talking about though??

1 Like

Oh, I didn’t answer correctly lol :sweat_smile: