Help with TimeOfDay

I want to check if the TimeOfDay in-game is >= 13 and <= 17.

I get issues with the <= 17 part, it says


(it says line 14 because there is another script above it in the same script, it works fine, i will mark line 14 in the script)

It spams this in output and doesn’t stop.

any help is appreciated

script, normal script in serverscriptservice

game.Lighting.Changed:Connect(function()
	if game.Lighting.TimeOfDay >= 13 and game.Lighting.TimeOfDay <= 17 then -- line 14
		game.Workspace.Natura.NaturaLock2.Transparency = 1
		game.Workspace.Natura.Pool.Sparkles.Sparkle.Enabled = true
		game.Workspace.Natura.Pool.Light.PointLight.Enabled = true
	else
		game.Workspace.Natura.NaturaLock2.Transparency = 0
		game.Workspace.Natura.Pool.Sparkles.Sparkle.Enabled = false
		game.Workspace.Natura.Pool.Light.PointLight.Enabled = false
	end
end)

TimeOfDay is a string. Try to compare ClockTime instead.[Which is a number].

ClockTime wouldn’t give me results as accurate as TimeOfDay would

Read the post I provided you.
Although ClockTime can be annoying sometimes, it should work. Since you’re only comparing, so say the TimeOfDay is now 13:30:00, and ClockTime shows 13.5 and a few more digits following the 5. This shouldnt bother you, it should work as expected.

If you still want to use TimeOfDay,
then use string.split() and get the first 2 digits and use tonumber on it.

I’ll try and implement ClockTime instead of TimeOfDay, thank you!

so would I do something like

if game.Lighting.ClockTime >= 13 and <= 17 then

Yes, you should give it a try.

If you still want to use TimeOfDay,
then use string.split() and get the first 2 digits and use tonumber on it.

Also, a tip:

local Lighting = game:GetService("Lighting")

is better to do over game.Lighting

1 Like
local lightning: Lighting = game:GetService("Lighting")
local natura = workspace.Natura
local naturaLock2: BasePart = natura.NaturaLock2
local sparkle: Sparkles = natura.Pool.Sparkles.Sparkle
local pointLight: PointLight = natura.Pool.Light.PointLight

lightning.Changed:Connect(function()
	if lightning.ClockTime >= 13 and lightning.ClockTime <= 17 then
		naturaLock2.Transparency = 1
		sparkle.Enabled = true
		pointLight.Enabled = true
	else
		naturaLock2.Transparency = 0
		sparkle.Enabled = false
		pointLight.Enabled = false
	end
end)