I currently have a script that counts down from ten minutes (60 * 10 seconds) and displays it on the client’s screen (like a timer) however, this shows the time in seconds, and not minutes then seconds. Is there any way so instead of the timer showing 600 seconds (10 minutes), it will instead show something like 10:00?
Hey there!
Lets say the remaining time (in seconds) is local remainingTime and the string, that will be placed as a text onto your TextLabel will be local text. The code will look something like this:
local minutes = math.floor(remainingTime/60)
local seconds = remainingTime - (minutes * 60)
if #seconds < 2 then
seconds = "0"..tostring(seconds)
end
local text = tostring(minutes)..":"..tostring(seconds)
I hope you get the point of this code, all you need to do is just to put the code into your while loop and set Text of your TextLabel to text.
An even better solution would be to use some format specifiers.
local function digital_format(seconds)
return string.format("%d:%02d", math.floor(seconds/60), seconds%60)
end
for i = 600, 0, -1 do
print(digital_format(i))
wait(1)
end
%d means digit, whereas %02d means if the number is not 2 digits long, then a 0 will be added to the beginning, which accounts for the seconds being 0-9. So it will look like 5:09 and not 5:9 or something.
local remainingTime = 86401
while true do
wait(1)
remainingTime = remainingTime - 1
local minutes = math.floor(remainingTime / 60)
local hours = math.floor(minutes / 60)
local seconds = remainingTime - (minutes * 60)
if string.len(seconds) < 2 then
seconds = "0"..tostring(seconds)
end
if string.len(minutes) < 2 then
minutes = "0"..tostring(minutes)
end
local text = tostring(hours)..":"..tostring(minutes)..":"..tostring(seconds)
print(text)
end
The minutes are huge (Specifically: 1440 ). They should not be more than 59.
Yeah, i was beginning to spend hours doing calculations on how to get a hour in a fraction and drove myself insane. too tired.
Here’s a workaround using incapz method
local Goal = os.time() + 86400 --24 hours
function HoursConversion(tim)
return("%02i:%02i:%02i"):format(tim/3600, tim/60%60, tim%60)
end
I quickly threw this script together - basically for the hours it does division, for the minutes it gets the remainder of seconds in an hour and divide by 60 minutes, and lastly for the seconds it gets the remainder of the hour and the minutes.
local Time = math.random(60 * 60, 60 * 135); -- Generate a random number between 3600 (1 hour) and 8100 (2 hours 15 minutes)
print("Time in seconds:", Time);
local Hours = math.floor(Time / 3600); -- Divide by 3600 (number of seconds in an hour)
local Minutes = math.floor((Time % 3600) / 60); -- Get the remainder of 3600, then divide by 60 (number of minutes in an hour)
local Seconds = math.floor((Time % 3600) % 60); -- Lastly get the remainder of 3600, then get the remainder from that (60 being the number of seconds in a minute)
print(string.format("Hours: %d\nMinutes: %d\nSeconds: %d",
Hours, Minutes, Seconds));
--[[
Time in seconds: W
Hours: X
Minutes: Y
Seconds: Z
--]]