I’ve been recently looking into how to use os.clock to make a countdown to events that is accurate to every user’s timezone. Every day at 12:00 PM, 3:00PM, 6:00PM, and 9:00 PM I host an event called awards where players in my group get bonus xp. Many young kids don’t understand timezones, however, and repeatedly will ask my moderators to convert the time to awards or even assume the EST scheduled times are in their timezone and ask why awards aren’t happening. To solve this I wish to code a sign that will display the time until these events, but I can’t wrap my head on how to accomplish this from what I’ve read on os.clock documentation. Any explanations on how a script like this would work are appreciated.
You can use os.time
and os.date
to achieve what you want, here are the docs for them: os.date, os.time.
The script will look like something like this:
It’s a prototype of the script, you can modify it to match your case.
local function getNextEventTime(currentTime)
local eventTimesUTC = {
12 * 3600, -- 12:00 PM UTC (adjust these times according to UTC)
15 * 3600, -- 03:00 PM UTC
18 * 3600, -- 06:00 PM UTC
21 * 3600 -- 09:00 PM UTC
}
-- get the current UTC time
local currentUTC = os.time(os.date("!*t"))
-- calculate today's start time in UTC (midnight)
local todayStartUTC = currentUTC - (currentUTC % 86400)
for _, eventTime in ipairs(eventTimesUTC) do
local eventTimeToday = todayStartUTC + eventTime
if eventTimeToday > currentUTC then
return eventTimeToday
end
end
-- if no event is left today, return the first event time of the next day
return todayStartUTC + 86400 + eventTimesUTC[1]
end
local function updateCountdown()
local currentUTC = os.time(os.date("!*t"))
local nextEventUTC = getNextEventTime(currentUTC)
local timeUntilEvent = nextEventUTC - currentUTC
local hours = math.floor(timeUntilEvent / 3600)
local minutes = math.floor((timeUntilEvent % 3600) / 60)
local seconds = timeUntilEvent % 60
local countdownText = string.format("%02d:%02d:%02d", hours, minutes, seconds)
-- i'll put print in here, but you can update your GUI or sign with countdownText
print("Next event:", countdownText)
end
-- so we call the function in every frame (you can use while True in here but I think RunService will be better)
game:GetService("RunService").RenderStepped:Connect(updateCountdown)
Let me know if it helps.
1 Like
Thanks! This is exactly what I was looking for.
1 Like
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.