Since the hours only matter, you can take the minutes out of the equation with ClockTime, and make sure you round it down:
local hours = math.floor(Lighting.ClockTime)
To get our minutes we can use the substring you used, but with just the minutes. This can be a string because we're not doing anything to it:
local minutes = Lighting.TimeOfDay:sub(3,5)
Now we need to know whether it's AM or PM, and the best way to do this is see if hours is above 12. If it is above 12, it's PM. If it isn't, it's AM. Also, I added a space so it's easier to put together down the road:
local meridiem = hours >= 12 and " PM" or " AM"
Then we can use a nifty little operator called the Modulus (or %). If you take a modulus of two numbers, it gives the remainder as if they were divided. For example:
100 (%) 10 = 0
12 (%) 10 = 2
10 (%) 3 = 1
3 (%) 4 = 3
So using this we can take the hours and modulate it by 12:
local displayHours = hours % 12
Now since 0 % 12 = 0 and 12 % 12 = 0, we're gonna need to fix that because there's no Zero'O'clock:
if displayHours == 0 then displayHours = 12 end
Then we just paste the string together!
local finalTime = tostring(displayHours) .. minutes .. meridiem
Here's the whole code combined with yours:
local Lighting = game.Lighting
while true do
local hours = math.floor(Lighting.ClockTime)
local minutes = Lighting.TimeOfDay:sub(3,5)
local meridiem = hours >= 12 and " PM" or " AM"
local displayHours = hours % 12
if displayHours == 0 then displayHours = 12 end
local finalTime = tostring(displayHours) .. minutes .. meridiem
script.Parent.TextLabel.Text = finalTime
wait(0.1)
end
EDITS: Clarification and formatting
2 comments
You shouldn’t use a while loop, instead use a changed event.
Thank you. I have since then updated the code.