Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to get the date for making live events?

Asked by
U_srname 152
5 years ago

So, the thing is I've heard of os.date() but I don't know how to use it.

I looked online but no sources were helpful. Maybe there is another way to get the date.

I'm trying to make a live event, and some code that would check if it is the date of the event. If so, then do event. If not, don't do event.

Thanks!

0
os.date requires one of the two arguments: "!*t" or "*t". This will make the function return a table that can be indexed with many keywords, like "day," "year," "month," "sec," and "hour." Fifkee 2017 — 5y
0
um U_srname 152 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

I'll show an example here of using os.date first. I have some suggestions at the end.

local dateTable = os.date("!*t") -- !*t and *t are magical "format strings". !*t means give me the
-- date in the UTC timezone (basically london's timezone), whereas *t will give the date based
-- on the current systems local timezone, this will refer to the roblox server so it's unclear what
-- that will really mean.

if dateTable["year"] == 2019 and dateTable["month"] = 7 and dateTable["day"] = 4 then
-- happy fourth of july
else
-- not doing the event :(
end

That check will obviously have to go in a loop. If you want something more granular in terms of time (maybe your event lasts only 3 hours) look into the os.date documentation here for what else you can find in the dateTable ... https://developer.roblox.com/en-us/api-reference/lua-docs/os.

What I would end up suggesting instead is that if this is a one-off event then you can compute the timestamp ahead of time (something used in computer science to signify a moment in time by the number of seconds elapsed since the "UNIX Epoch" (https://en.wikipedia.org/wiki/Unix_time)) and then check os.time() against it. os.time() gives you the number of seconds. What I like about this is it becomes much clearer how to do time intervals. For example, say I want a july fourth celebration this year to start at midnight in the UTC timezone and last for 10 hours after that. I went to a website https://www.unixtimestamp.com/ and found out that the timestamp I want it to start at is ... 1562198400 (seconds since the unix epoch).

while true do
if os.time() >= 1562198400 and os.time() <= 1562198400 + 10 * 60 * 60 then
-- party code
else
-- no party code
end
wait()
end

EDIT: An epiphany. We don't actually need to do loops to wait for the right time, we can actually use wait with this last method here ...

local servertime = os.time() -- the initial time
local eventstart = 1562198400 -- what time I want to wait for
local diff = eventstart - servertime
if diff > 0 then -- if it hasn't happened yet
wait(diff) -- no loop needed!
end
print("woop woop")
-- celebrate until diff == -60 * 60 * 10 or soemthing ...
Ad

Answer this question