I have this round script that counts down from 5 minutes and resets upon reaching 0. But I would like to have the timer display in minutes and not seconds, as the script just shows Time: 300. Is there anyway to do this?
You could make a function using the modulus symbol (%
) that returns the number of minutes of a time, to actually make it display the minutes and seconds on a timer, you would need to do a lot more complicated functions with string formatting and math, which I don’t really want to do now.
Quick function I wrote in VSCODE:
local time = 1
function ConvertClock(time)
if time ~= 1 or time ~= 0 then
return time / 60
else return time end
end
print(ConvertClock(time))
It works decently, I recommend modifying it to fit your needs.
It says this in output: attempt to perform arithmetic (mod) on function and number
try renamed the variable time
to something else (Not the parameter), since time
is a function.
Yes I did that but the same result
Hold on, let me boot up roblox studio, I’m using Lua 5.4 on VSCODE, it might have something to do with the version of Lua I’m working with.
It’s working perfectly for me, I’m not sure what’s happening to you.
I put the code at the start of the script after the variables. Do you think it is because of this?
Maybe something like this could work:
function Timer(seconds)
for i = seconds, 0, -1 do
local minutes = math.floor(i/60)
local sec = math.floor(i%60) -- Optional
--return string.format("%02i:%02i", minutes, sec)
RemoteEvent:FireAllClients(string.format("%02i:%02i", minutes, sec))
end
end
Edit: I made a tiny error which was I returned the value which would stop the for loop I believe. Instead of that just send it to your clients or the text you want to show.
local minutes = math.floor(time / 60)
local seconds = time - math.floor(minutes * 60)
print(minutes..":"..seconds)
time
is the variable of raw seconds (300 for instance)
If you want just a converting function, then you could do something like this:
local e = 130
function ConvertClock(Time)
if Time ~= 1 or Time ~= 0 then
local minutes = math.floor(Time / 60)
local seconds = math.floor(Time % 60)
return minutes, seconds
else return Time end
end
print(ConvertClock(e))
My code is outclassed, very sad, yes I took “inspiration” from @TheDCraft.
The timer still looks the same with the function and I use a server script
Yeah so what you would do is use the function and the timer would look something like this: 05:00
instead of 300
. Do you have any images of the code or how the timer looks?
the timer is just a gui with text and the script updates the timers text
What does the timer show after you use the function?
You need to make the GUI’s text a combined string of the minutes and seconds.
local e = 130
function ConvertClock(Time)
if Time ~= 1 or Time ~= 0 then
local minutes = math.floor(Time / 60)
local seconds = math.floor(Time % 60)
return {minutes, seconds}
else return Time end
end
print(ConvertClock(e)[1]..ConvertClock(e)[2])
--[[
Or you could do:
<GUI>.Text = tostring(ConvertClock(e)[1])..":"..tostring(ConvertClock(e)[2])
]]
It shows the time format in seconds
Do you have an image. I will change the function to only show minutes:
function Timer(seconds)
for i = seconds, 0, -1 do
local minutes = math.floor(i/60)
RemoteEvent:FireAllClients(string.format("%02i:00", minutes))
end
end
Try something like this:
function ConvertClock(Time)
if Time ~= 1 or Time ~= 0 then
local minutes = math.floor(Time / 60)
local seconds = math.floor(Time % 60)
return {minutes, seconds}
else return Time end
end
for i = 1, 120 do
<GUI>.Text = tostring(ConvertClock(i)[1])..":"..tostring(ConvertClock(i)[2])
task.wait(1) -- Make it wait however you want.
end
It give an error saying expected assignment or function call