Script can't find the time

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
1 Like

are you calling the function by doing CheckTime()

Think so?

-----character limit-----

if you’re calling the function somewhere after the function then it should work, can we see more of from this script?

yeah i think he didnt call the function

1 Like

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.

if thats all the code in the script, yea ur not calling the function, thats why theres not even error because it wasnt called in the first place

1 Like

if you wanna debug something, try putting print(“test”) in lines u wanna see if it reaches there

ah, i see.

–character limit–

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()

What are you talking about?

The if statement works just fine.

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

1 Like

if statement works just fine, because 18+ is night and so is 6 below, atleast the way the sky is

1 Like

It’s an and-or, on Roblox. Either A or B, or both.

That has fixed it. However, it doesn’t update it if the time does change.

I’d run your loop outside of the function, and just call the function.

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:

Lighting:GetPropertyChangedSignal("ClockTime"):Connect(aFunction)

Changed Events would be much more efficient. And take less resources than a loop.

1 Like

I doubt it. Clocktime changes every frame afaik

I have done that and there has been no change.

btw, where is this script located at?

Works fine for me

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() 

5b8a7a3864e16cd643bdc052ef837a6e