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
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
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
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
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
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.
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