Hey developers, so I have a daylight cycle script in my game but I would like to make a GUI which acts like a digital clock and displays the current clock time so it is in time with the daylight cycle, the problem with using the clock time property of lighting though is that you can end up with decimals, and using math.floor() for example will usually provide you with just the integer which defeats the point of the clock design I am going for with displaying minutes in addition to displaying the hour, is there an alternative method I could use?
1 Like
this will only show the hours
while wait(10) do
gui.text = game.lighting.ClockTime..":00"
end
did it work?
1 Like
local t = game.Lighting:GetMinutesAfterMidnight()
local hours = math.floor(t / 60)
local minutes = t % 60
print(string.format("%02d:%02d", hours, minutes))
Hours would be in military time, though. If you want 12-hour time just do
hours = (hours - 1) % 12 + 1
1 Like
I never tried thisβ¦
while true do
wait()
textbox.text = game.lighting.TimeOfDay
end
1 Like
you can use math.round
instead of math.floor
like this
local lighting = game:getService("Lighting")
local t = lighting:GetMinutesAfterMidnight()
local hours = math.round(t / 60)
local minutes = t % 60
print(string.format("%02d:%02d", hours, minutes))
With this you want to truncate it to the nearest integer. Otherwise it might round up by an entire hour, like here:
local lighting = game:getService("Lighting")
lighting:SetMinutesAfterMidnight(839) -- 13:59
local t = lighting:GetMinutesAfterMidnight()
local hours = math.round(t / 60)
local minutes = t % 60
print(string.format("%02d:%02d", hours, minutes)) -- 14:59
print(lighting.TimeOfDay) -- 13:59
1 Like
t % 60-- gets the decimal part?
math.floor(t / 60)
takes the total minutes, divides by 60 to get hours, then throws away the remainder since we just want the number of hours.
To get minutes, you use the remainder from that division, (you get it with t % 60
) and that is the number of minutes since the last hour.
1 Like
cant you just do
print( hours..":"..minutes)
Yes but using the string pattern formats the numbers to 2 digits, so instead of, say, β10:7β it would say β10:07β
1 Like