Help on how to detect if the weather is day or night

Hi, devs,

So, I’m trying to make a street lamp that detects when it’s daytime or nighttime. So, when it’s daytime, it turns off and when it’s nighttime it turns on. Now, bear with me. I am a terrible scripter and my script may be very horrid. This is what I did:


local light = script.Parent
local lightsource1 = light.PointLight
local lightsource2 = light.SpotLight
local timeofday = game.Lighting.ClockTime

if timeofday == 17.6 then
	lightsource1.Enabled = true
	lightsource2.Enabled = true
	
else if timeofday == 6.3 then
		lightsource1.Enabled = false
		lightsource2.Enabled  = false
	end
	
end

For some reason, it does not work and it can be a very idiotic issue that I did not notice. Can anyone help, please? :smiley:

Much thanks!!
Aki

3 Likes

you have got the variable ClockTime and stored it to another variable, I understand how you got confused but thats not how variables work, you arent storing the variable you are storing what the value of ClockTime was at the time which wont help

2 Likes

try this:

local light = script.Parent
local lightsource1 = light.PointLight
local lightsource2 = light.SpotLight
local lighting = game.Lighting

if lighting.ClockTime == 17.6 then
	lightsource1.Enabled = true
	lightsource2.Enabled = true
	
else if lighting.ClockTime == 6.3 then
		lightsource1.Enabled = false
		lightsource2.Enabled  = false
	end
	
end
2 Likes

I just discovered a problem. Since my daycycle script does not specifically go on those values, the lights won’t do anything. I’ve been trying to temper around like… greater then or less then symbols, whatever. Still no use…

1 Like

This is the script I tried now:

local light = script.Parent
local lightsource1 = light.PointLight
local lightsource2 = light.SpotLight
local lighting = game.Lighting

if lighting.ClockTime < 17.6 then
	lightsource1.Enabled = false
	lightsource2.Enabled = false
else if lighting.ClockTime > 17.6 then
		lightsource1.Enabled = true
		lightsource2.Enabled = true

if lighting.ClockTime < 6.3 then
		lightsource1.Enabled = true
			lightsource2.Enabled  = true
		else if lighting.ClockTime > 6.3 then
				lightsource1.Enabled = false
				lightsource2.Enabled = false
			end
		end
	end

end

Umm… There’s my version:

local light = script.Parent
local lightsource1 = light.PointLight
local lightsource2 = light.SpotLight
local lighting = game.Lighting

lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
    if lighting.ClockTime >= 17.6 or lighting.ClockTime <= 6.3 then
        lightsource1.Enabled = true
        lightsource2.Enabled  = true
    end
end)
4 Likes

So, I fixed it up a bit because you forgot to state when it turns off. I’m still giving you the solution. This is the finalized script:

local light = script.Parent
local lightsource1 = light.PointLight
local lightsource2 = light.SpotLight
local lighting = game.Lighting

lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
	if lighting.ClockTime >= 17.6 or lighting.ClockTime <= 6.3 then
		lightsource1.Enabled = true
		lightsource2.Enabled  = true
		
	else if lighting.ClockTime <= 17.6 or lighting.Clocktime >=6.3 then
			lightsource1.Enabled = false
			lightsource2.Enabled = false
		end
	end
end)

I still would like to give you the solution because I used the same method you did but gave it the opposite ‘Greater than, Less than’ state.

Thanks a lot for the help! :smiley:

3 Likes

Yes i forgot about that but you don’t need to use elseif. You can just use else because it only turns on at night.

2 Likes