# Help with lighting

Why does this not work?

``````if game.Lighting.ClockTime == 0 then
game.Workspace.ClockHand1.Material = Enum.Material.Neon
game.Workspace.ClockHand2.Material = Enum.Material.Neon
game.Workspace.ClockHand1.Color = "25, 25, 255"
game.Workspace.ClockHand2.Color = "255, 255, 255"

elseif game.Lighting.ClockTime == 7 then
game.Workspace.ClockHand1.Material = Enum.Material.SmoothPlastic
game.Workspace.ClockHand2.Material = Enum.Material.SmoothPlastic
game.Workspace.ClockHand1.Color = "199, 84, 84"
game.Workspace.ClockHand2.Color = "147, 62, 62"
end
``````
3 Likes

What specifically is the problem?

The if statement does not work at all

Do you have a day/night cycle? If so, how far are you changing the time? If youâ€™re tweening it with a rate, then itâ€™s not guaranteed that the tween will hit 0 or 7 exactly.

1 Like
``````local dayLength = 5

local cycleTime = dayLength*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
``````
1 Like

Instead of setting times equal to 0 and 7 exactly, you can leave a margin so it assures that the time will hit that margin.

``````if game.Lighting.ClockTime >= 23.5 and game.Lighting.ClockTime <= 0.5 then
game.Workspace.ClockHand1.Material = Enum.Material.Neon
game.Workspace.ClockHand2.Material = Enum.Material.Neon
game.Workspace.ClockHand1.Color = "25, 25, 255"
game.Workspace.ClockHand2.Color = "255, 255, 255"

elseif game.Lighting.ClockTime >= 6.5 and game.Lighting.ClockTime <= 7.5 then
game.Workspace.ClockHand1.Material = Enum.Material.SmoothPlastic
game.Workspace.ClockHand2.Material = Enum.Material.SmoothPlastic
game.Workspace.ClockHand1.Color = "199, 84, 84"
game.Workspace.ClockHand2.Color = "147, 62, 62"
end``````
1 Like

Awesome! Let me try this. ` ` ` ` ` `

Itâ€™s midnight in game and absolutely nothing is happening for some reason. Could it be cause itâ€™s not the correct type of script? Iâ€™m quite confused.

Full script:

``````local dayLength = 5

local cycleTime = dayLength*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

if game.Lighting.ClockTime >= 23.5 and game.Lighting.ClockTime <= 0.5 then
game.Workspace.ClockHand1.Material = Enum.Material.Neon
game.Workspace.ClockHand2.Material = Enum.Material.Neon
game.Workspace.ClockHand1.Color = "25, 25, 255"
game.Workspace.ClockHand2.Color = "255, 255, 255"

elseif game.Lighting.ClockTime >= 6.5 and game.Lighting.ClockTime <= 7.5 then
game.Workspace.ClockHand1.Material = Enum.Material.SmoothPlastic
game.Workspace.ClockHand2.Material = Enum.Material.SmoothPlastic
game.Workspace.ClockHand1.Color = "199, 84, 84"
game.Workspace.ClockHand2.Color = "147, 62, 62"
end
``````

The script isnâ€™t reaching the code because of the repeat loop. Itâ€™s a forever loop. Put the repeat loop inside a coroutine.

``````local dayLength = 5

local cycleTime = dayLength*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

coroutine.wrap(function()
repeat
local currentTime = tick()

if currentTime > endTime then
startTime = endTime
endTime = startTime + cycleTime
end

lighting:setMinutesAfterMidnight((currentTime - startTime)*timeRatio)
wait(1/15)
until false
end)()

if game.Lighting.ClockTime >= 23.5 and game.Lighting.ClockTime <= 0.5 then
game.Workspace.ClockHand1.Material = Enum.Material.Neon
game.Workspace.ClockHand2.Material = Enum.Material.Neon
game.Workspace.ClockHand1.Color = "25, 25, 255"
game.Workspace.ClockHand2.Color = "255, 255, 255"

elseif game.Lighting.ClockTime >= 6.5 and game.Lighting.ClockTime <= 7.5 then
game.Workspace.ClockHand1.Material = Enum.Material.SmoothPlastic
game.Workspace.ClockHand2.Material = Enum.Material.SmoothPlastic
game.Workspace.ClockHand1.Color = "199, 84, 84"
game.Workspace.ClockHand2.Color = "147, 62, 62"
end``````

Ahh, I see. Thank you! Let me try.

Assuming the issue is related to the if statement not running when it should run(on script startup and when the time changes) or to the fact you canâ€™t set a part color through a string(you must use Color3.fromRGB for your case) the following code should fix it, also it gets rid of unnecessary/repeating code:

``````function onTimeChanged()
local t = game.Lighting.ClockTime
local c = Color3.fromRGB --you need to use Color3 when changing an instance color
--combining the if statements to avoid repeating steps
if t == 0 or t == 7 then
local m = (t == 0 and Enum.Material.Neon) or Enum.Material.SmoothPlastic
--you can use the variable workspace instead of game.Workspace
workspace.ClockHand1.Material = m
workspace.ClockHand2.Material = m
workspace.ClockHand1.Color = (t == 0 and c(25, 25, 255)) or c(199, 84, 84)
workspace.ClockHand2.Color = (t == 0 and c(255, 255, 255)) or c(147, 62, 62)
end
end

--check the time on script startup and every time it changes
onTimeChanged()
game.Lighting:GetPropertyChangedSignal("ClockTime"):Connect(onTimeChanged)
``````

PS: Also as @Corruptonator mentioned, if your day/night cycle doesnâ€™t set the clock time to an integer when it updates it(or it has a chance of skipping a full integer, for example going from 0.69 to 0.71) you should adapt the conditions so they match a range of time, however, that time must be a bit less than the â€śstepâ€ť of each clock iteration, else the code might run multiple times(for example if the time moves forward 0.5 at a time your range should be less than 0.5 but almost that much).

Ooh, yeah. Iâ€™ll try this one since Corâ€™s didnâ€™t work unfortunately

Wait. Clarification: Do i combine this with Corâ€™s script or keep it alone?

Okay! Wow! It worked absolutely perfectly! WOOHOO! The 2nd clock hand didnâ€™t become neon for some reason though. @NyrionDev

It may be neon but you canâ€™t notice it because of the color.

The first clock hand is Neon Red.
The second clock hand is Red. I think Iâ€™d notice.

Well, thatâ€™s strange because my code sets both part materials to `m` which means that every time the time is set to 0 or 7 they will have the same material(neon for 0 and smooth plastic for 7).

Perhaps another script or part of the code changes the material?

I assume itâ€™s probably because burgundy is too dark. I made the color lighter.

Okay, problem. Can you help me edit this script to make it reach 0 and 7 directly so it can light up?

``````local dayLength = 5

local cycleTime = dayLength*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

coroutine.wrap(function()
repeat
local currentTime = tick()

if currentTime > endTime then
startTime = endTime
endTime = startTime + cycleTime
end

lighting:setMinutesAfterMidnight((currentTime - startTime)*timeRatio)
wait(1/15)
until false
end)()
``````