Script not working to make a timer with os.date

Hey everyone! So I’ve tried making a script to make an in-game timer to work until 8pm UTC on October 6th, 2019. My script is here:

local day = os.date("*t")["day"]
local month = os.date("*t")["month"]
local hour = os.date("*t")["hour"]
local minute = os.date("*t")["minute"]
local second = os.date("*t")["second"]

while true do
	if day < endDay.day then
		if hour < endDay.hour then
			if minute < endDay.minute then
				if second < endDay.second then
					print(endDay.day - day .. ":" .. endDay.hour - hour .. ":" .. endDay.minute - minute .. ":" .. endDay.second - second)
				end
			end
		end
	end
	wait(1)
end

but my output doesn’t include this print line.

Any help is appreciated. Thanks!

(it’s in ServerScriptService in a ServerScript.)

2 Likes

The first problem is that the table that os.date returns doesn’t have “minute” or “second” fields. They’re actually “min” and “sec”.

It would probably be easier for you to compare if os.time() is over or equal to 1570392000 (Sunday, October 6, 2019 8:00:00 PM GMT). You can easily generate this number as well. Just google “unix epoch time converter” and it should be the first result.

You want to do the following instead:

local DateTable = os.date("!*t", os.time())

local day = DateTable.day
local month = DateTable.month
local hour = DateTable.hour
local minute = DateTable.min
local second = DateTable.sec

Hey there!

So I combined your strategies, and this is what I came up with.

local endDay = {month = 10, day = 5, hour = 8, min = 0, sec = 0}
local DateTable = os.date("!*t", os.time())

local day = DateTable.day
local month = DateTable.month
local hour = DateTable.hour
local minute = DateTable.min
local second = DateTable.sec

script.Day.Value = day
script.Hour.Value = hour
script.Minute.Value = minute
script.Second.Value = second

while true do
	if day < endDay.day then
		if hour < endDay.hour then
			print(endDay.day - day .. ":" .. endDay.hour - hour .. ":" .. endDay.min - minute .. ":" .. endDay.sec - second)
		end
	end
	wait(1)
end

Now, it print’s this, though.

  3:8:-8:-55

Any ideas? Thanks so much! :slight_smile:

What exactly are you trying to print?

The current time left until October 6th, 2019 at 8pm.

You can subtract the os.time() number from 1570392000 (October 6th at 8 PM GMT) in order to get the number of seconds remaining. You can then do math in order to get the days, hours, minutes, and seconds remaining.

1 Like

Hey! So I’m going to use this. Thanks!

P.S. Is there a way to remove the decimal from a number? i.e. I have 5.503189, can I remove the 503189 (or however many seconds are left exactly) through code to just have 5?

Yes there is! You can use math.floor() to round down, and math.ceil() to round up.

Sweet! So one more thing, how can I do that? (On the wiki, it says I must choose x, which is what it must round below/exactly to.)

math.floor(5.234) → 5
math.ceil(5.234) → 6

Nevermind, but thanks! Sorry, I thought it meant I had to know & put the number down. Thanks so much for your help! :slight_smile: