How would you make a countdown to January 1st, 2020 at 12 AM?

Hello,

I am trying to make a countdown to January 1st, 2020 countdown in Roblox, but it seems that I’m having the problem of having the daysLeft at 0. After reading this and this topic, I had a little bit of better understanding on how to do this, so I tried and, just like every game I make on Roblox, failed. The value of daysTill is 0, yet it should be 91 days. Here is my code and most of it is from this topic.

local newYearsDay = os.time({year=2020; month=1; day=1})
local secondsDiff = os.difftime(newYearsDay,os.time())

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)

repeat
	wait()
	script.Parent.Time.Text = daysTill.." days until New Years."
until daysTill == 0

Thanks for reading! :slight_smile:

I’d recommend using https://developer.roblox.com/en-us/api-reference/lua-docs/os#date to display this correctly :slight_smile:

The first part of your code looks fine.

3 Likes

Hello,

When I click the link it goes to the website os.date instead of the dev hub. Might wanna fix that. :slight_smile: Anyway, I’m trying this out.

Thanks!

Hello,

And, seems like I’m getting an error with my code.
Error:

Workspace.Part.SurfaceGui.Script:33: attempt to call local 'daysTill' (a number value)

Code:

repeat
	wait()
	script.Parent.Time.Text = os.date("*t" ,os.time(daysTill()))
until daysTill == 0

Thanks! :slight_smile:

daysTill is a number, not a function. You’re calling it :stuck_out_tongue:

1 Like

Thanks for letting me know about this, lol.

:slight_smile:

Now, I’m getting a different number, yay. Fun.

Error:

Workspace.Part.SurfaceGui.Script:34: bad argument #3 to 'Text' (string expected, got table)

Code:

local newYearsDay = os.time({year=2020; month=1; day=1})
local secondsDiff = os.difftime(newYearsDay,os.time())

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)

repeat
	wait()
	local daysTill2 = tostring(daysTill)
	script.Parent.Time.Text = os.date("*t" , daysTill2)
until daysTill == 0

Thanks! :slight_smile:

According to the wiki, os.date reutens a table.

You should correct it with this:

script.Parent.Time.Text = tostring(os.date("*t" , daysTill2).day)
1 Like

This seems to have solved the error, but it seems that to the game, there is only 31 days until new years! YAY! But really, there are only 91. Aww. :frowning:

Here is my code:

local newYearsDay = os.time({year=2020; month=1; day=1})
local secondsDiff = os.difftime(newYearsDay,os.time())

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)

repeat
	wait()
	local daysTill2 = tostring(daysTill)
	script.Parent.Time.Text = tostring(os.date("*t" , daysTill2).day)
until daysTill == 0

Please read the wiki page.

Field Type Description
day int An integer between 1 and 31 describing the current day of the month.

You’re going to have to do some math to calculate it.

1 Like

I’d also like to add on:

What is the purpose of setting the text until daysTill == 0? daysTill isn’t updating so you’re just wasting performance by doing this.

Also, try and do this to fix it:

script.Parent.Time.Text = tostring(os.date("*t" , daysTill2).yday)
1 Like