I am attempting to create a light that turns on during a time range (18:00 to 6:00)
It doesn’t seem to work. There aren’t any errors. I have added a loop in hope that that’s the issue but it seems to struggle finding the time.
Code:
local Lighting = game:GetService("Lighting")
local Light = script.Parent
local Beam = Light.Beam
local SL = Light.SpotLight
function CheckTime() -- Code
while true do
if Lighting.ClockTime >= 18 or Lighting.ClockTime <= 6 then -- Time values
Light.Transparency = 0
Beam.Enabled = true
SL.Enabled = true
wait(1)
else
Light.Transparency = 0.6
Beam.Enabled = false
SL.Enabled = false
wait(1)
end
wait(1)
end
end
That’s it.
There’s probably a very obvious error now think about it. My scripting knowledge was never the best, and taking a break certainly doesn’t help.
The issue with your code is in the condition for checking the time. You are using an “or” operator instead of an “and” operator.
The condition if Lighting.ClockTime >= 18 or Lighting.ClockTime <= 6 will always evaluate to true since the clock time can’t be both greater than 18 and less than 6 at the same time.
To fix this, you should change the “or” operator to “and” operator.
Here’s the corrected code:
local Lighting = game:GetService("Lighting")
local Light = script.Parent
local Beam = Light.Beam
local SL = Light.SpotLight
function CheckTime()
while true do
if Lighting.ClockTime >= 18 or Lighting.ClockTime <= 6 then
Light.Transparency = 0
Beam.Enabled = true
SL.Enabled = true
else
Light.Transparency = 0.6
Beam.Enabled = false
SL.Enabled = false
end
wait(1)
end
end
CheckTime()
and just adds to the statement. Is like saying this for example: if you have Object 1, and Object 2 then do this
or is adding another condition to look for, which is saying this for example: if you have the key to the first Door, or a Token to pass to the next room, do this
Because you need to add a Changed Event to check for when the time changes, you can use GetPropertyChangedSignal for this and assign it to only fire when ClockTime changes:
local Lighting = game:GetService("Lighting")
function CheckTime()
for i = 1,10 do
if Lighting.ClockTime >= 18 or Lighting.ClockTime <= 6 then
print("a") else
print("b") end
wait(1)
end
end
CheckTime()