How do i show the current game time in a textlabel

so i wanna show the ClockTime in a textlabel the only problem is that it has too many decimals and also the time is in 24-hour format i want it to be like am/pm instead of 24-hour format and i dont know how to do it

here is the day/night cycle code

-- dayLength defines how long, in minutes, a day in your game is. Feel free to alter it.
local dayLength = 12

local cycleTime = dayLength*60
local minutesInADay = 24*60

local lighting = game:GetService("Lighting")

local startTime = tick() - (lighting:getMinutesAfterMidnight() / minutesInADay)*cycleTime
local endTime = startTime + cycleTime

local timeRatio = minutesInADay / cycleTime

if dayLength == 0 then
	dayLength = 1
end

repeat
	local currentTime = tick()
	
	if currentTime > endTime then
		startTime = endTime
		endTime = startTime + cycleTime
	end
	
	lighting:setMinutesAfterMidnight((currentTime - startTime)*timeRatio)
	wait(1/15)
until false

here os the textlabel code

while wait() do
	script.Parent.Text = tostring(game.Lighting.ClockTime)
end
3 Likes

Using TimeOfDay property would be easier in my opinion. Do you want to use TimeOfDay property instead (it is easier to make am / pm version of it)?

could you explain i didnt understand exactly what you said

ClockTime looks like this: 14.5
TimeOfDay looks like this: 14:30:00

oh thx now i get it but now ive got it i dont know how to remove the seconds from TimeOfDay since timeofday is 14:29:81 so idk how i can remove the seconds and also how i convert it to a 12-hour time format also i suck at time stuff

It is possible by using string manipulation. I will make a script soon to explain it.

oh thanks also my weakness in scripting is string manipulation,math and also time

This script changes time and prints time in am / pm format:

local lighting = game:GetService(“Lighting”)
while wait(.1) do
game.Lighting.ClockTime += 0.1

local splittedTime = lighting.TimeOfDay:split("")
local hours, minutes
for i, v in pairs(splittedTime) do
	if i == 2 then
		if splittedTime[1] == "0" then
			splittedTime[1] = ""
		end
		
		if tonumber(splittedTime[1] .. splittedTime[2]) <= 12 then
			hours = splittedTime[1] .. splittedTime[2] .. " am"
		else
			hours = splittedTime[1] .. splittedTime[2] .. " pm"
		end
		
	elseif i == 5 then
		if splittedTime[4] == "0" then
			splittedTime[4] = ""
		end
		
		minutes = splittedTime[4] .. splittedTime[5]
		print("hours: ", hours)
		print("minutes: ", minutes)
		break
	end
	
end

end

thanks ill try soon :slight_smile:

Hours later I realized that I forgot one thing - 13 pm doesn’t exist.

If you also didn’t notice that or you couldn’t fix that, there is my fixed version of the script:

local lighting = game:GetService("Lighting")
while wait(.1) do
	game.Lighting.ClockTime += 0.1
	
	local splittedTime = lighting.TimeOfDay:split("")
	local hours, minutes
	for i, v in pairs(splittedTime) do
		if i == 2 then
			if splittedTime[1] == "0" then
				splittedTime[1] = ""
			end
			
			if tonumber(splittedTime[1] .. splittedTime[2]) <= 12 then
				hours = splittedTime[1] .. splittedTime[2] .. " am"
			else
				hours = tonumber(splittedTime[1] .. splittedTime[2]) - 12 .. " pm" -- This got changed
			end
			
		elseif i == 5 then
			if splittedTime[4] == "0" then
				splittedTime[4] = ""
			end
			
			minutes = splittedTime[4] .. splittedTime[5]
			print("hours: ", hours)
			print("minutes: ", minutes)
			break
		end
		
	end
end

I have looked at the script in studio and it works fine for me, Where is your script located in explorer?

I Tested it with a script in ServerScriptService

I put the script in ServerScriptService.

And its still not working? Because as I said earlier I put

local lighting = game:GetService("Lighting")
while wait(.1) do
	game.Lighting.ClockTime += 0.1
	
	local splittedTime = lighting.TimeOfDay:split("")
	local hours, minutes
	for i, v in pairs(splittedTime) do
		if i == 2 then
			if splittedTime[1] == "0" then
				splittedTime[1] = ""
			end
			
			if tonumber(splittedTime[1] .. splittedTime[2]) <= 12 then
				hours = splittedTime[1] .. splittedTime[2] .. " am"
			else
				hours = tonumber(splittedTime[1] .. splittedTime[2]) - 12 .. " pm" -- This got changed
			end
			
		elseif i == 5 then
			if splittedTime[4] == "0" then
				splittedTime[4] = ""
			end
			
			minutes = splittedTime[4] .. splittedTime[5]
			print("hours: ", hours)
			print("minutes: ", minutes)
			break
		end
		
	end
end

in a regular script in ServerScriptService

It works correctly for me after updating my script. It doesn’t matter where is the script or if it’s local script or not, as long as the script can run, it will always work.

Ok i see now where you updated your script, i did not see that earlier sorry

thx for all the help and with some changes i got the perfect results!!!

local lighting = game:GetService("Lighting")
while wait(.1) do
	game.Lighting.ClockTime += .1
	local splittedTime = lighting.TimeOfDay:split("")
	local hours, minutes
	local AmOrPm
	for i, v in pairs(splittedTime) do
		if i == 2 then
			if splittedTime[1] == "0" then
				splittedTime[1] = ""
			end

			if tonumber(splittedTime[1] .. splittedTime[2]) <= 12 then
				hours = splittedTime[1] .. splittedTime[2]
				AmOrPm = " am"
			else
				hours = tonumber(splittedTime[1] .. splittedTime[2]) - 12 -- This got changed
				AmOrPm = " pm"
			end

		elseif i == 5 then
			if splittedTime[4] == "0" then
				splittedTime[4] = ""
			end

			minutes = splittedTime[4] .. splittedTime[5]
			if string.len(minutes) == 1 then
				minutes = "0"..minutes
			end
			print("hours: ", hours)
			print("minutes: ", minutes)
			script.Parent.Text = hours..":"..minutes..AmOrPm
			break
		end

	end
end

changed script ^

so much thx to @Spiderr12PL and @XmcElectricity