How to make a script run a function every 6 Hours interval

How do I make a script fire a function off every 6 hours real time.

So when it’s 6:00pm it triggers, when it’s 12pm it triggers etc

1 Like

Use os.time, the rest you can figure out

2 Likes

Just do this:

local Hours = 6

	while wait(3600 * Hours) do
		print("Hello")
end
2 Likes

That’s not real time. That’s per server.

1 Like

Every few seconds, check the time using DateTime. I recommend using UTC.

-- Example
while task.wait(4) do
    local now = DateTime.now():ToUniversalTime()
    -- Fetch the current time in UTC

    local hour = now.Hour
    if (hour % 6) == 0 then
       -- Checks if the hour is divisible by 6
       -- This means if the time is 12 or 6 PM, it will pass the check

       -- Do something 
    end
end
2 Likes

Ty, how do I also get time remaining? So the time remaining till the next 6 hour. I.e ‘3:42:50s Remaining…’

local waittime = DateTime.fromUnixTimestamp(DateTime.fromUniversalTime(2023,10,1).UnixTimestamp-DateTime.fromUnixTimestamp(DateTime.now().UnixTimestamp).UnixTimestamp):ToUniversalTime()

Messy, but only way i could make it use universal

3 Likes

Thanks, how does this code work? What is 2023,10,1 and is this remaining time till the next ‘Sixth’ hour?

1 Like

year, month, day, hour. minute, second are the parameters

1 Like

Ty, how do I make it get the time till the next 6th hour? so I.e if it’s 3pm, then it’d be 3 hours till the next 6th hour (6PM). And if it’s 7 pm it’d be 5 hours till the next sixth hour (12)

1 Like

I don’t really know to be honest, this is the only thing i could help with sorry

1 Like

This may be it …

while true do
	local now = DateTime.now():ToUniversalTime()
	local hour = now.Hour
	local minutes = now.Minute
	local seconds = now.Second

	local timeUntilNextSixHour = 6 - (hour % 6)
	local minutesUntilNextSixHour = timeUntilNextSixHour * 60 - minutes
	local secondsUntilNextSixHour = minutesUntilNextSixHour * 60 - seconds

	if (hour % 6) == 0 then
		print("It's a 6-hour mark!")
		-- Do something special if it's a 6-hour mark
	else
		local hoursRemaining = math.floor(secondsUntilNextSixHour / 3600)
		local minutesRemaining = math.floor((secondsUntilNextSixHour % 3600) / 60)
		local secondsRemaining = secondsUntilNextSixHour % 60

		print(string.format("Time until next 6-hour mark: %02d:%02d:%02d", hoursRemaining, minutesRemaining, secondsRemaining))
	end

	task.wait(1)
end

Hey, thanks very much.
I have a question, why is the 1 hour when the 6th hour is in a few minutes?

Hey, how can I also check for the next 30 mins? I would like to open something once the 6 hour mark is reached, but that thing stays open for only 30 mins, so 30 mins after then it will close. I would like it so the 30 min is UTC so every server shares the same 30 min check

You got me, going off omega’s code. It triggers if hour is divisible by 6.
This would be better than when they start the program …
I take it, it would fire at the actual 6 and 12 time wise.

Since it’s based on UTC time, it’s fetching the time from UTC. If you want to use it for local time, you can use:

local now = DateTime.now():ToLocalTime()

The reason why I recommended UTC is because it’s the central clock (but you don’t have to use it if you don’t need to)

thanks, i will use the utc one.

how can I also check for the next 30 mins in UTC?
I would like to open something once the 6 hour mark is reached, but that thing stays open for only 30 mins.

so I need to display the countdown to the next 30 mins after then it will close. I would like it so the 30 min is UTC so every server shares the same 30 min check

something like this

image