How do i make a live countdown?

I wan’t to create a countdown for a specific date to create a live event, but i don’t know how to do it, i saw some threads and the dev hub but they are not helping me in any way. I hope you can help me, Thanks. Any question you can reply me :+1:

5 Likes

os.time() is a very useful function that gives you time in seconds since the epoch in UTC (sometimes referred to as GMT).

If you know the time of the event, you can take away the result of this function and you’re left with the number of seconds.

You could display this, though if it’s longer than a few minutes it’ll be a big number that is meaningless to people, so you then want to break it up into minutes (60), hours (3600), days (86400), maybe even further…

Check out this site to grab the epoch time of any date you like: https://www.epochconverter.com/

2 Likes

My live event script is using os.date(), i prefer using os.date() than os.time().

1 Like

You can utilise os.date to get the current year, day, hour etc of the time given.
If no time is given then it is defaulted to os.time.

Now keep in mind "*t" is the string format for localtime, now you’ll probably want to use UTC time for that, which is !*t .

Putting that into action, you can have a set amount of seconds of which is the UNIX time of your event and then subtract the current os.time, finally pass that into os.date to get your statistics.
Here’s an example:

local newYear = 1577836800

while true do
    wait(1)
    local statistics = os.date("!*t", newYear - os.time())
    print(string.format("There is %d years, %d months, %d days till new year", 
    statistics.year, statistics.month, statistics.day))
end

I used this time converter for getting UNIX time:

8 Likes

Could you explain me the script? there are stuff like the local newYear which i dont know how to use, or why you are using %d. And if there’s another way to make this? Another easier way to manage this?

1 Like

The variable, newYear, is the UNIX time for 00:00:00 1/1/2020.
I used it as an example.
If you’re uncertain about UNIX time, it’s the amount of seconds since 1/1/1970 00:00:00.


%d is the string pattern for any digit, it will be substituted for the number given.

1 Like

Well, i already made a live event script:

local EventTime = os.date(!*t)

while true do
wait(1)
if EventTime.Year = 2019 and EventTime.Month = 12 and EventTime.Day = 25 then
script.Parent.Disabled = false --Event handler
end
end

And i want to have the same code with a countdown.

1 Like

Yep, it’s possible to integrate this into your current code.
I got the UNIX time of your event, you can make that a variable:

local eventUnixTime = 1577232000

Then subtract the EventTime and put it in a os.date:

local eventCountdown = os.date("!*t", eventUnixTime - os.time())

Finally, you can use all the different properties:

print(string.format("There's %d days till Xmas!", eventCountdown.day))

FYI: These variables and your EventTime variable should be defined inside the while loop, otherwise they never change!

14 Likes

But there’s a way to do this countdown without Unix time?

1 Like

There are no other methods that I can think of or either I have heard about that can do this.

There should be no problem in using UNIX time.

1 Like

The script is confusing, and i can’t manage it very correctly, but i will let your reply as solution.

2 Likes

You could look at the many other threads created on this specific topic, which you should’ve done before posting! :mask: They may give you an extra perspective of how these work. You can also do your own research and dissect the code to further understand what’s happening.

https://devforum.roblox.com/search?q=countdown%20to%20date%20category%3A55

1 Like

I already saw some threads, i just said that on the main comment. And i also made my own researchs, but i’m someone with a poor experience of scripting.

The only way those threads couldn’t have helped you is if you didn’t read them properly or you don’t have a sufficient grasp on scripting which you should first obtain rather than making duplicate threads. If you don’t understand how things come together from the start, systems like this at going to be very difficult for you to use, let alone assemble.

Take some time to learn scripting, as it’s vital to understanding what you’re writing. It’s easier to just be given code but it’s much more useful to be able to understand what is going on so that you can modify and write to your needs. You’ll be able to better understand what you’re working with and that could even lead to better questions about the parts of your system.

1 Like

Scripting is not an easy obstacle, of course i’m taking time to learn and trying to find copyable places which i can use to learn scripting. I’m someone who is really bad at reading, maybe i just skipped something but i’m still trying to find anything that it could help me to create this. I try to always learn, for some reason I have published this thread, otherwise I would not.

Is this a good way to solve the problem? I mean, you just wait until the event is on. Is there no other way to use wait() less exessively?
I just want to put such a system into my game myself and let the player see a countdown three days before it starts.

1 Like