How to indentify TimeOfDay?

I think this doesn’t work with the if statement… How am I suppose to identify certain times of the day?

if game.Lighting.TimeOfDay == "8:00:00" then
print("It's 8AM")
end

Perhaps try using ClockTime, it will return the time in hours, and is not a string.

1 Like

Are you changing the time of day or is it set to 8:00:00 when you load into the game? Because this will only fire once, when the game first loads, so you might want to add a wait(5) at the beginning.

1 Like
if game.Lighting.ClockTime >= 13 then
     print("It's " .. math.floor(game.Lighting.ClockTime - 12) .. "PM")
elseif game.Lighting.ClockTime < 13 then
     print("It's " .. math.floor(game.Lighting.ClockTime) .. "AM")
end

Try this script I just wrote. It checks if the ClockTime is greater than or less than 12, therefore determining the AM or PM state. It rounds to the last hour, as your statement seemed to have rounded from 8:00:00 to 8.

game.Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function(clocktime))
     if clocktime >= 13 then
          print("It's " .. math.floor(clocktime - 12) .. "PM")
     elseif clocktime < 13 then
          print("It's " .. math.floor(clocktime) .. "AM")
     end
end)

Put the script in a change detection function and it will print the ClockTime if it ever changes.

2 Likes

Use the ClockTime Property. It will say the hour and how much % of it has passed. For example, 8:30:00 would be 8.5 in ClockTime.
And this returns a number value instead of a string.

1 Like

Hello everyone. So i’ve been experimenting with this code in my game, but its keep returning an error, saying i’m trying to compare a nil value to a number (the if statement in the script). Any reason why it wouldn’t work?

I placed the script in the workspace, it is a server script.

The original code didn’t work because Instance:GetPropertyChangedSignal's passed event doesn’t include a parameter with it. For example:

game.Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function(clocktime)
	print(clocktime)
end)

Doesn’t work because the event doesn’t include the value of the changed property, it’s just a simple event. To fix this, we can simply access the ClockTime directly:

game:GetService("Lighting"):GetPropertyChangedSignal("ClockTime"):Connect(function()
	local clocktime = game:GetService("Lighting").ClockTime
	if clocktime >= 13 then
		print("It's " .. math.floor(clocktime - 12) .. "PM")
	else -- an elseif statement isn't necessary here, so just use else
		print("It's " .. math.floor(clocktime) .. "AM")
	end
end)
3 Likes

Yes, thank you @Inconcludable for pointing that that out. I shall use it to my advantage. :happy1: