How to make a countdown timer - Roblox

How to create a roblox countdown timer


Here is how you can make a countdown timer for roblox. Here are some photos of what we’ll be creating today:

First creating UI.


Making this UI is super simple, first of course we need to design the Ui, if you don’t want to make your own you can download mine here (43.8 KB).

Apart from that this is how you’re going to structure you UI:

image

Make sure you have a localscript and not a script! Then ensure it’s inside of the “StarterGui” folder.

Scripting


Now we can start with scripting, it’s really important you ensure you’re using a local script and not a script.

If you use a script your timer will not work.

Now that we’ve created our localscript I want you to paste this code in, each line is commented so you know what the script does.

local text = script.Parent.Text --Defines a variable with our text. 

local TimeToStart = 1670622031 --Defines  a variable with our time to start the event 
local TimeToStop = 6666666666666666666666666  --Define a variable with our time to stop the event 
 
function DisplayTime(countdowntime) --Creates a function that will display on the UI how much time is remaining

	local futuretime = os.clock()+countdowntime --This winn define a variable that gi ves us the future time.

	local timeleft = futuretime --This defines how much time we have left 	
	timeleft = math.abs(futuretime - os.clock()) --Returns the abseloute value of the fututetime - the current time

	local hours = string.format("%0.2i", math.floor(timeleft/3600)) --Calculates how many hours are remaining

	local hs = timeleft % 3600

	local mins = string.format("%0.2i",math.floor(hs/60)) --Calculates how many minutes are remaining

	local secsleft = string.format("%0.2i",hs%60) --Calculates how many seconds are remainding. 

	script.Parent.Text = "Time remaining:".. hours..":"..mins..":"..string.format("%0.2i",math.floor(secsleft)) --Displays how much time is remaining through a textlabel.
end

while wait(1) do --Every second this loops run.
	local TimeNow = os.time() --This gets the current time now


	if TimeNow >= TimeToStop then --This if statement checks if the time now is larger or equal to the time to stop
		text = "Event completed!"  --If the above is true then it will set the event to "Event Completed"
		break
	end

	if TimeNow >= TimeToStart then --This if statement checks if timenow is larger or equal to timetostart
		text = "Event started!" --This is what will appear at the end of the timer
		break
	else
		local CountdownTime = TimeToStart-TimeNow --This will give how much time is remaining
		DisplayTime(CountdownTime) --This will execute the function and pass the countdowntime.
	end
end

Your TimeToStart is when your event will start and your TimeToStop will prevent the countdown from starting if somebody joins a new server and the event is already over.

This countdown timer uses unix time, a great unix time converter can be found at Unix Time Stamp - Epoch Converter.

Outro


If you have any questions about this system feel free to ask down below, if you have any bugs and you don’t know how to fix them just open a #scripting-support

Thanks for reading and that’s all from me! :wave:

1 Like