How do I define the time of day and display the minutes?

QUESTION 1. I am trying to make the GUI show the time of day on the GUI. I already have a day night cycle, showing hours and minutes. But I can’t figure out how to make the time of day definition?

I mean this: (time of day)
from 0:00 to 3:00 - night
from 3:00 to 12:00 - morning
from 12:00 to 16:00 - day
from 16:00 to 24:00 - evening (and here the cycle repeats again and again…)

QUESTION 2. I also can not figure out how to make the minutes in the GUI displayed correctly (and not just as a number.) Just if the minutes are in the interval from 1 to 9, they will be displayed as normal numbers, but not in the format 00, 01, 02, 03 and so on. Also tried to make a spacing condition, but I don’t understand how to make double inequalities in LUAU. The syntax swears at this :confused:

HELP!

local Gui = --Put your gui here

function getLightingTime()
local totalMinutes = game.Lighting:GetMinutesAfterMidnight()
local hours = math.floor(totalMinutes / 60)
local minutes = math.floor(totalMinutes % 60)
local period
if hours < 12 then
period = “AM”
else
period = “PM”
hours -= 12
end
if hours == 0 then
hours = 12
end
return string.format("%02d:%02d %s", hours, minutes, period)
end

while true do
Gui.Text = getLightingTime()
wait()
end

1 Like

You just need to restructure the if elseif statements just like that:

local ClockTime = Lighting.ClockTime
local DayType: string

if ClockTime <= 3 then      -- from 0 to 3 (it can't be negative)
    DayType = "Night"
elseif ClockTime > 3 and ClockTime <= 12 then   -- from 3 (3.000001) to 12
    DayType = "Morning"
elseif ClockTime > 12 and ClockTime <= 16 then   -- from 12 (12.000001) to 16
    DayType = "Day"
elseif ClockTime > 16 and ClockTime <= 24 then   -- from 16 (16.000001) to 24
    DayType = "Evening"
end

You can check if the minutes value is less than 10 by replacing the first statement to:

if Minutes < 10 then
    Real = "0" .. tostring(Minutes)
end
1 Like

Thank you so much. You helped me!

For the gui thing you could make 3 or 4 Seperate text labels and place them close to each other to make it look like its 1 text, For the minutes you could figure out how many seconds the minutes are in your game, when you figure it out then put a local script in all of your text labels and put this code:

local text = 6 – put 12 if the game is 12PM when you join it.

while wait() do

text=text+1 – after the amount of seconds you put, the text will multiply by 1

script.Parent.Text = text

wait(120) – how many seconds its gonna take for the text to change
end

Thank you too! I’m going to test your version.

1 Like

@EgizianoEG @Apasanskiy
This is actually a Very inefficient way to check time. there are a lot of better ways to check the time rather than using elseifs, I’ll give an Example:

It would be a lot Easier to use string.format to concatenate instead of using .. as that can make your script look weird.
To Put Simply:

% is something to replace
%s means a string
%d means a number (Preferablly an Integer)

However, the Item you want is %02i which will add a 0 Digit to your number, so if you had the Number 1 and format it to %02i, it will give you 01, if your number is ten, this will return 10 , you will have to add another Digit to the format so: %03i and so on, this will give you 010, this is useful so you don’t have to use a bunch of if statements to add a 0 to your String:

This should be the Format if I’m Correct:

string.format("Day %d|%02i:%02i|%s", Days, Hours, Real, DayType)

Printing it will give you these Results:

Summary
print(string.format("Day %d|%02i:%02i|%s", 2, 1, 5, "Hello")) -- "Day 2|01:05|Hello"
print(string.format("Day %d|%02i:%02i|%s", 2, 12, 50, "Noon")) --"Day 2|12:50|Noon"

Plus, Its Generally better to swap out the if statements and use a table:

local Times = { 
["Morning"] = 0; -- 12 AM
["Dawn"] = 6; -- 6 AM
["Noon"] = 12; -- 12 PM
["Evening"] = 13; -- 1 PM
["Afternoon"] = 15; -- 3 PM
["Night"] = 20; -- 8 PM
}

So, In this case, we can do:

local DayTime
local ff = -1 -- found Format (Used to assist the for loop in finding the format)
for dt, x in Times do
    if Hours >= x and Hours > ff then -- checks the number and gets DayType
        DayTime = dt -- new DayType
    end
end

So now you have a good system to use! What now, then you should format your code from the example above.

2 Likes

I agree, but he wanted a fix for the code he provided not an entirely different method of it.

2 Likes

Wow! Thank you! Your code was the best. And the explanation too!

It isn’t exactly a Different system, its mainly improvements upon Concatenations and ifs/elseifs

Hi again. I checked for the full cycle of the day and found a bug. For some reason only “Morning” “Noon” “Afternoon” is shown and the script does not show the rest. Can you figure out what the reason is?

you can probably change the limit if the Number finds another number bigger than what it has, so you would add ff = Hours to do so, so it should be this:

    if Hours >= x and Hours > ff then
        DayTime = dt -- new DayType
        ff = Hours -- This makes it so the loop has to look through a new limit
    end
1 Like

I’m probably boring you already, but it’s not working again. This time it started showing only 2 values Dawn (almost all day) and Morning (the first 3 hours in the game).

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.