Scripting Countdown Help

Hi everyone, so, i’d like to make a countdown with, days, hours, mins and secs. But i only now how to do mins and secs. My scripting skills are very poor. If anybody could help me to make a countdown with days, hours, minutes and seconds for a live event id be very greatful.

2 Likes

Can you send us the code please so we can help you with that.

1 Like

sure, let me search it out, and ill send it

1 Like

image

2 Likes

i can put it at 120mins for 2h, but it doesnt look good, and the countdown its for aprox 3-4 days, so it would be a huge amount of mins

1 Like

I did something like that for a group rewards function that I created. What this loop is doing is counting down from 12 hours. Hope this may help.

2 Likes

let me test it, and ill tell u, thx btw ;D

1 Like

So, i reviewed the script a few times and i don’t really understand it. (Has I said my scripting skills are very very basic) So isn’t a way of modifying the script I sent and add an hours and days countdown?

1 Like

For a live event, you’d use Unix Time to predetermine the time of the event and just loop every second to update the timer! :slight_smile:

local TimeOfEvent = 000000000-- Unix Time of Event, there's plenty of sites that you can just put a date and time in and it'll tell you this!

local function toHMS(s) -- Function that returns days hours minutes seconds
	return ("%02i Days, %02i Hours, %02i Minutes, %02i Seconds"):format(s/86400, s/60^2%24, s/60%60, s%60)
end

local function DoEvent()
	print("Time's up!")
end

while true do wait(1) -- update time every second

	local timeUntil = TimeOfEvent - os.time() -- Get difference in time between now & time of the event
	print(toHMS(timeUntil)) -- Convert the difference from above to d/h/m/s format  using toHMS function & print it (or do whatever you want to do with it)
	
	
	if timeUntil <= 0 then
		DoEvent() -- Make this a function to perform your event
		break
	end
	
end
3 Likes

thx, im gonna try it, and see how it goes :smiley:

okay, im done with the test, the script works, i added the unix time of the event. The time gets printed in the output. But i need to make it in a GUI, so the countdown is visible. But im already thankful about the script, thx

1 Like

If it’s for a Billboard or Surface GUI you can just update the TextLabels Text in the same script.

If it’s in a ScreenGui just run it on the server to perform the event, and in a Local Script in the GUI (excluding the DoEvent() function) to update the TextLabels Text.

1 Like

okay…(sorry if im annoying, but my scripting skills are very basic and poor). How do i update TextLabels Text in the same script?

Just change line 15 to locate your GUI. :smiley:

local TimeOfEvent = 00000000000-- Unix Time of Event, there's plenty of sites that you can just put a date and time in and it'll tell you this!

local function toHMS(s) -- Function that returns days hours minutes seconds
	return ("%02i Days, %02i Hours, %02i Minutes, %02i Seconds"):format(s/86400, s/60^2%24, s/60%60, s%60)
end

local function DoEvent()
	print("Time's up!")
end

while true do wait(1) -- update time every second

	local timeUntil = TimeOfEvent - os.time() -- Get difference in time between now & time of the event
	
	game.Workspace.part_the_gui_is_in.BillboardGui_or_SurfaceGui.TextLabel.Text = toHMS(timeUntil) -- Change part_the_gui_is_in to the part where the gui is located, and the billboardgui_or_surfacegui to which one it is
	
	if timeUntil <= 0 then
		DoEvent() -- Make this a function to perform your event
		break
	end
	
end



3 Likes

Thank you so much, the Countdown is working perfectly, i made it into the SurfaceGui and it works, im very greatful, tysm :smile:

2 Likes

I would like to point out that for any timer that requires a high degree of precision, relying on wait(1) to count down is a bad idea. Take a look at the documentation for wait - it does not guarantee that the delay is exactly the amount of time you tell it to yield, as it depends on the framerate and other factors.

A pretty unknown feature of wait is that it actually returns the amount of time waited, which may be useful for keeping track of time like so:

local TimeElapsed = 0
while true do
	local YieldTime = wait(1)
	TimeElapsed += YieldTime
end

It is also important to consider that the other code within your loop may take time to execute as well, so even with this measure, you’ll still face inaccuracies which will propagate over time. The best way to avoid this is to simply keep track of the start time and simply compare it to the current time whenever updating the timer. Here’s an example of that:

local StartTime = tick()
local TimeElapsed = 0
while true do
    wait(1)
	TimeElapsed = tick() - StartTime
end
2 Likes