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

How would I change the time from seconds to minutes?

Asked by 10 years ago
while true do
    for i = 120, 0, -1 do
        status.Value = "Time left: " ..i
        wait(1)
    end
end

This script counts down from 120 seconds to zero. How would I make the 120 look like 2:00 minutes?

2 answers

Log in to vote
2
Answered by 10 years ago

You can use division to see how many minutes are left and then modulo or %to find out the seconds left.

Remove The space between % 02d. The site will not show them if they are together.

while true do
    for i = 120, 0, -1 do
        status.Value = ("Time Left: %d:% 02d"):format(math.floor(i/60),i%60)
        wait(1)
    end
end

Edit: Updated script....Thanks DataStore

2
Another way that you could achieve the "0 before the number when it's less than 10" is through the use of string.format(). In this user's code you could implement this by replacing lines 4 and 5 with the following: hastebin.com/hutizacoba.rsl - I would post it here, but for some reason the website keeps removing the parts before 'd'. DataStore 530 — 10y
0
Thanks both of you rabidhalofan12345 55 — 10y
Ad
Log in to vote
0
Answered by 7 years ago

not sure if this would work because it just popped up, but im pretty sure it should work, we will need the integer division to find how many 60 seconds are within 120 seconds or any time length that is in seconds

local TimeLength = script.Parent.TimeLength --[[ just to get the time length (in seconds) of the parenting sound--]]

local seconds = 0
local minute = 0
-- variables that will get the result

minute = TimeLength // 60
-- how many 60s are there in timelength without floating point? integer division (//) does the trick

seconds = TimeLength - minute
--get the remaining seconds left before the next minute

--FINALLY print the results (the .. part joins strings together in case you dont know what it does)
print("the sound is ".. minute .. " minutes and " .. seconds .. "seconds long")

the output: "the sound is 2 minutes and 0 seconds long"

Answer this question