New Year Countdown - Time Frame Scripting

Hey developers!

As few know, I am making a new game called “New Year’s Bash”. And as many of you know, I am still becoming a little more aware of scripting.

I just wanted to know a little more about scripting countdowns, and clocks.

What I am looking to learn a little more about, is how to set up the correct times.
Example:

while true do Wait(1)

wait()

local Time = (135 * 24 * 60 * 60) - Tick()

local Days = mFloor((Time / 60 / 60 / 24) % (365 + 0.2425))

local Hours = mFloor((Time / 60 / 60) % 24)

local Minutes = mFloor((Time / 60) % 60)

local Seconds = mFloor(Time % 60)

If anyone knows how I can find the correct time frame to fit my exact date and time, that’d be very helpful.

Thank you!

4 Likes

So, if you want to calculate the amount of days there are till say, the first of January (New Years day) you can simply get it’s amount of seconds from epoch and get the difference between now and then. So, something like this:

local newYearsDay = os.time({year=2019; month=1; day=1}) --Setting the date to January 1st, 2019 12 AM
local secondsDiff = os.difftime(newYearsDay,os.time()) --Get the amount of seconds between now and New Years day

Now if you want to stop the loop all you really need is the secondsDiff and do a simple <=0 check.

Although, I do see you want the days, hours, minutes, and seconds till that special moment in time as well.
Again, something like this:

local secondsTill = secondsDiff%60
local minutesTill = math.floor(secondsDiff%3600/60)
local hoursTill = math.floor(secondsDiff%86400/3600)
local daysTill = math.floor(secondsDiff%(86400*30)/86400)
10 Likes

I’ll give this a try!

1 Like

Another question to ask yourself is in what capacity is this being used? Is it for a game, or a group event?

os.time() will calculate the time from Epoch based on UTC, which is the GMT timezone; this is good if you want the timer to be synchronized across all users, but can become a source of error if using this for a planned event at midnight EST and the game says New Years happened 5 hours ago; simply shifting the time to match midnight in the desired timezone to GMT solves this.

Conversely, if this were a small gimmick for a game with multiple servers you may want to use tick (for the first time in its existence) to have a better chance at matching the user’s local timezone as tick is dependent on the server’s timezone, not UTC. However this is a relative gamble as the server could be misaligned to the users timezone.

An additional note for Discourse formatting.

You can format your code in Discourse by surrounding it with three ```'s or on both sides of the text to make it become color formatted and easier to read:

```Lua
print(“Hello World!”)
```

or

[code]
print(“Hello World!”)
[/code]

results in:

print("Hello World!")

Discourse uses BBCode and Markdown for formatting so if you want to read-up on what your options are you can check them out here and here.

local function toHMS(s)
	return ("%02i:%02i:%02i"):format(s/60^2, s/60%60, s%60)
end
2 Likes