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

How to make a countdown that countdowns 1 month across the server?

Asked by 6 years ago
Edited 6 years ago

Basically, I have a countdown on my Shop's SurfaceGui. It's supposed to countdown from one month, but I have issues with it. I don't want the countdown to display 2592000 seconds left, I want it to display like 30 days, 15 minutes, 3 seconds left. Something like that. Also, I want to run it in the background so it still countdowns when no players are on the game basically. I know how to do it in remote events, because Ghoulstem answered my last question about how to countdown across the server. Also, I'm bad at explaining things. Hopefully this makes sense. Thank you!

1 answer

Log in to vote
3
Answered by 6 years ago
Edited 6 years ago

Time


Time is weird. It's no coincidence that you don't see many people using months as a unit of time. This is because when we use the term "month", it's very relative. One month from now may have taken longer than one month prior, and vise versa. This differs from more accurate units of time such as days (though days do constantly change in time, it's mostly negligible and we're able to just call it 24 hours) and constants like hours, minutes, and seconds. Long story short, use days, weeks, or years for long-term periods of time instead of months. It's easier and just makes more sense.

Format


The format of how you want to display this time counting down is very important. It's not just getting how many days, hours, minutes etc individually, you want to make sure they stay relevant to each other. Otherwise you just have 4 units of time giving you the exact same thing. Luckily, this is pretty easy to do and the math is pretty elementary. There's also a very useful function called format (of Lua's string library) which we'll use to combine our countdown with a nice display showing us the units of time.

Example of format

print(string.format("I have %d cookies", 10)) -- > "I have 10 cookies"

It's pretty much a way of inserting variables in a string using string patterns and assigning those variables a value in the second argument(s) of string.format.

Seconds


Since we'll be keeping track in Unix time, our total time constant will be represented in seconds. Converting between seconds, minutes, and other units of time is pretty simple arithmetic. There are 60 seconds per minute, therefore any number of seconds divided by 60 yields x number of minutes, and so on with other units. We'll be using this logic for our formatting.

Implementation


Without keeping you waiting any longer, this is how we can implement all of the information that was just covered. I believe the commentary in the code should be a clear enough explanation, but if it's not, feel free to leave a comment and I'll get back to you as soon as possible.

-- Some variables for library functions
local ostime = os.time
local format = string.format
local floor = math.floor

local function start_countdown(t)
    -- Capture current time
    local now = ostime()

    -- Add current time to total time in seconds (the multiplication just converts each respective unit into seconds)
    local time = now + t.days*86400 + t.hours*3600 + t.minutes*60 + t.seconds

    -- No point in starting the counter if the amount of time is less than or equal to the current time, now is it?
    if time <= now then
        print("Timer cannot be set to less than or equal to current time")
        return
    end

    -- Repeat the counter while the current time is still less than the set time (and wait one second in between each interval)
    while ostime() < time and wait(1) do

        -- Get the current time, and difference between current time and set time (the "delta time")
        local now = ostime()
        local delta = time - now

        -- Get the units of time relative to each other (make sure to floor them so you get a nice whole number)
        local min = floor(delta/60)
        local hour = floor(min/60)
        local day = floor(hour/24)

        -- Finally, format the calculated units with a string displaying the information
        local timer = format(
            "Time left: %d Days, %d Hours, %d Minutes, %d Seconds",
            day, hour%24, min%60, delta%60
        )

        -- Whatever you choose to do with the information. This will just output the timer and the format.
        print(timer)
    end
end

-- Passing the argument to the function with the timer information
start_countdown{days = 30, hours = 0, minutes = 0, seconds = 0}

You'd do the same thing when nobody is in your server, just getting the difference in time is all you need to tell how much time has passed. If you have any questions, just let me know.

0
One question. Where would I put this? PyccknnXakep 1225 — 6y
Ad

Answer this question