Using OS.Time to make something happen at midnight every day

Hi! I would like to make an event happen in my game every day at 12. Both AM and PM. The event part is no problem, but making it so that all servers are in sync when it happens is difficult. It doesn’t matter what time zone it’s in, as long as it happens every 12 hours. Thank’s in advance!

1 Like

every 12 hours? that would go from lets say 6:00 PM to 6:00 AM to 6:00 PM. You want it to be every 24 hours, 12:00 AM, 12:00 AM, 12:00 am. just saying

That’s what I meant. Thanks for pointing it out.

1 Like

Can you give us the clde you already have? Also, no matter what It’ll happen in every server at the same time when your using os.time, no need for extra code, its already automatic.

If you don’t mind if it’s up to slightly less than a second max for any server you encounter, you can use something like:

local firstTime = 1613511735
local numberOfTimesHappened = 0

while true do
	local currentTime = os.time()
	if numberOfTimesHappened == 0 then
		if currentTime >= firstTime then
	elseif currentTime >= firstTime * numberOfTimesHappened then

I ended up actually being able to do

function Format(Int)
	return string.format("%02i", Int)

function convertToHMS(Seconds) -- Converts seconds into hours, minutes, and seconds
	local Minutes = (Seconds - Seconds%60)/60
	Seconds = Seconds - Minutes*60
	local Hours = (Minutes - Minutes%60)/60
	Minutes = Minutes - Hours*60
	return Format(Hours)..":"..Format(Minutes)..":"..Format(Seconds)

while wait(0.5) do
	local Time = os.time()
	local date ="!*t", Time) --Gets the Universal Standard time as a table
	local secondsOfTheDay = date["hour"]*3600 + date["min"]*60 + date["sec"] -- Finds how many seconds have passed in the day
	local secondsLeftUntilMidnight = convertToHMS(86400 - secondsOfTheDay) -- There are 86400 seconds in a day, so this finds how much time is left.
	if 86400 - secondsOfTheDay == 0 then -- If there are 0 seconds left of the day then do stuff
	       -- Stuff

This goes off every 24 hours, which I can settle for. I could make it 12 hours but I think 24 hours would be better. In the real code I am using the ‘secondsLeftUntilMIdnight’ to set the text value of a gui label.


I just came across this right now and I’ve been looking for this for a while, However I want to do something a little different where the event happens more than once in a day. Is there any way I can get this to work? I’m still fairly new to Time ect. If you could help that would be amazing!