Text Lable Time Display UI Script Helping

I want to display the Time. The scrirpt does that but before it I want to say Time in the front and then the timeofday.

How would I do that.

Currant script

while true do
script.Parent.Text = string.sub(game.Lighting.TimeOfDay,1,5)
wait()
end

1 Like

Maybe try this?

while true do
      local Hours  = math.floor(game.Lighting:GetMinutesAfterMidnight()/60) --round hours 
      local Minutes = game.Lighting:GetMinutesAfterMidnight() - (Hours * 60) --take the amount of minutes already accounted for with the hours, then use that to get the minutes
      if Hours > 12 then
           Hours = Hours - 12
      end
      script.Parent.Text = "Time: "..Hours.." : "..Minutes
      wait(1)
end

Here is how it would work.
Say it is 1:30 P.M, then it would be 810 minutes after midnight

  1. Hours:
    We divide 810/60, and we get 13.5. Then we use math.Floor to round that to 13 hours.

2: Minutes:
13 hours * 60 = 780, so that means that the hours takes care of 780 of the minutes, so we need an extra 30 minutes to fill in the rest. Basically we calculate that there is 30 minutes on top of however many hours.

3: Convert to Pm and Am
13 hours is greater than 12 hours, so we subtract 12 from it to get 1.PM in hours.

  1. Finally concatenate the strings to get the final time.

Are you trying to use irl time, or ingame time? you can use os.time I’m guessing

The problom is it now works like this robloxapp-20210130-1603279.wmv (302.2 KB)

Sorry idk why but I was never able to see “Robloxapps” videos. Could you explain it in words or make it a gyazo, thanks. Also do you want in game time or OS time?

Basically its 2:45.6 then 1 second later 2:47.8 and then so on its hard to explain.

What is the difference between OS Time and Game Time?

OS time is like real “Earth” time, while Game Time is whatever time it is in the game (like how you can change it so each minute is an hour in the game). Sounds like you are using Game Time. Try this, I rounded the minutes so it will be cleaner:

while true do
      local Hours  = math.floor(game.Lighting:GetMinutesAfterMidnight()/60) --round hours 
      local Minutes = math.floor(game.Lighting:GetMinutesAfterMidnight() - (Hours * 60)) --take the amount of minutes already accounted for with the hours, then use that to get the minutes
      if Hours > 12 then
           Hours = Hours - 12
      end
      script.Parent.Text = "Time: "..Hours.." : "..Minutes
      wait(0.5)
end

Yep gametime.

Your script has sort of worked.

Could you edit the orginal script so it just says Time: In the front and try not to make correct minutes and hours just Army Time?

Wait so what do you want it to say, something like this:

Time: 5:40

Yes something like that correct

Isn’t that what it is already? Maybe just take out some space, changing this line:

To this:

   script.Parent.Text = "Time: "..Hours..":"..Minutes

Still does not work. Like I said I dont care about standred time any more. I just want to say Time: in front. Using this script:

I’m sorry I don’t think I understand what you want. When I tried it in my own game it worked fine for me. The only problem is that the when the minutes is less than 10 it looks weird, but that can be fixed pretty easily.

How would that be fixed? Thats the big problem

Basically, we check if minutes is less than 10. If it is we change the minutes number slightly… Like this:

while true do
	local Hours  = math.floor(game.Lighting:GetMinutesAfterMidnight()/60) --round hours 
	local Minutes = math.floor(game.Lighting:GetMinutesAfterMidnight() - (Hours * 60)) --take the amount of minutes already accounted for with the hours, then use that to get the minutes
	if Hours > 12 then
		Hours = Hours - 12
	end
	if Minutes < 10 then
		Minutes = "0"..Minutes
	end
	script.Parent.Text = "Time: "..Hours..":"..Minutes
	wait(0.3)
end

Yes that works. What is the point of the

at the end?

Oh that’s just how often it updates. It should be a wait of at least 0.1 to reduce lag, but anything from 0.1 to 0.8 or so should work fine.

Also if you don’t understand how this code works I can break it down.

Yes please break it down that would be very helpful.

1 Like

I’ll break it down line by line:


Line1: Just a loop that loops forever.


Line2: We are calculating the hours. GetMinutesAfterMidnight returns how many
 minutes we have after midnight. So basically we see how many minutes it is 
then we divide that by 60 to get the hours. Say it was 150 minutes after
 midnight, that would give us 2.5 hours, so we use math.floor to remove the 
decimals and convert 2.5 into 2. Math.floor just converts a decimal into a 
normal number by removing all decimals.


Line3: We take the amount of minutes, and then see how many of those minutes
 are already taken up by the hours. Say it was 150 minutes after midnight,
 we would take the 2 hours that we calculated above, and see that 120 minutes
 is already being used with the hours. That leaves us with 30 minutes, plus 
the 2 hours.


Lines 4 - 6: We check if the amount of hours is greater than 12, if it is
 then we subtract the hours by 12. For example, say we had 15 hours, 15 is 
greater than 12 so we subtract 12 from it to get 3 hours.


Lines 7 - 9: We check if the minutes is less than 10. If it is, then we add on a
 0 before it using ".." to concatenate it. Say we had 7 minutes, we do 
"0"..7 to get 07 minutes.


Line 10: Finally we just concatenate the entire thing together.

I hope that helps :slight_smile: !

1 Like