How would I fit putting this into a text box work?

So I recently got this script for a very kind and helpful person, but I have no clue as to how to put this into a text box and make it show up with the time, any help? Here is the script.

local Lighting = game:GetService("Lighting")

local hourRaw = tonumber(Lighting.TimeOfDay:match("^%d+"))
local hour12h = math.floor(hourRaw % 12)
local isPM = (hour / 12) >= 1

if hour12h == 0 then
    hour12h = 12
end

You need to form a string out of that data.

local formattedTime = ("%d:%d %s"):format(hour12h, "00", isPM and "PM" or "AM")
TextLabel.Text = formattedTime

This was included in my response, aside from how to fetch minutes.

Line 5 of the script, which is
`

local isPM = (hour / 12) >= 1

`

the “hour” is unknown.

Ohh, excuse me. Change that to this:

local isPM = (hourRaw / 12) >= 1

Corrected in the linked thread as well.

Now, the only issue im having here is, for my game, I have it set up so that its a build up knowning, when the next time is, any way to add minutes to that? Also, its stuck at 2pm?

I might change it to while true do.

Most likely because you did not add it into a loop of some sort and just copy-pasted the code into the script. This is a relatively primitive situation which you could fix by adding this in yourself as opposed to relying only on code I post. I don’t provide whole scripts to use, I provide segments that you need to modify to work for your use cases.

1 Like

I tried this, and it timed out, should I add a wait of a second or 2?

local Lighting = game:GetService("Lighting")

while true do

local hourRaw = tonumber(Lighting.TimeOfDay:match("^%d+"))

local hour12h = math.floor(hourRaw % 12)

local isPM = (hourRaw / 12) >= 1

if hour12h == 0 then

hour12h = 12

end

local formattedTime = ("%d:%d %s"):format(hour12h, "00", isPM and "PM" or "AM")

script.Parent.TextLabel.Text = formattedTime

end

Again, something else you could figure out on your own without me telling you. The script times out because your while loop doesn’t have a wait in it. Luckily, timeout behaviour does exist, because regularly this would have crashed Studio.

Yes, you should add a wait at the end of the loop.

1 Like

I will definetly edit this to add minutes, thank you so much for your help, this has been very helpful.