The lights don't turn off when the game reaches specific time

So basically, I wrote a script for the day/night cycle that works perfectly, but I also have stuff like lanterns and I want to make them turn off when it’s a day, and turn on when it’s a night.
I hope anyone would help me because I don’t get any errors in the output and I don’t know whats the problem.

lighting.ClockTime.Changed:Connect(function()
	if lighting.ClockTime == 17.667 then
		for i, v in pairs(Lanterns:GetDescendants()) do
			if v:IsA("MeshPart") then
				if v.Material == Enum.Material.Neon then
					v.Transparency = 0
					v.BrickColor = BrickColor.Cork
				end
			end
		end
		for i, v in pairs(HouseLights:GetDescendants()) do
			if v:IsA("Part") then
				if v.Material == Enum.Material.Neon then
					v.BrickColor = BrickColor.Cork
				end
			end
			if v:IsA("PointLight") then
				v.Enabled = true
			end
		end
	elseif lighting.ClockTime == 7 then
		for i, v in pairs(Lanterns:GetDescendants()) do
			if v:IsA("MeshPart") then
				if v.Material == Enum.Material.Neon then
					v.Transparency = 0.5
					v.BrickColor = Color3.fromRGB(0, 0, 0)
				end
			end
		end
		for i, v in pairs(HouseLights:GetDescendants()) do
			if v:IsA("Part") then
				if v.Material == Enum.Material.Neon then
					v.BrickColor = Color3.fromRGB(0, 0, 0)
				end
			end
			if v:IsA("PointLight") then
				v.Enabled = false
			end
		end
	end
end)

Try :GetPropertyChangedSignal
https://developer.roblox.com/en-us/api-reference/function/Instance/GetPropertyChangedSignal

1 Like

if lighting.ClockTime == 17.667 then

If ClockTime doesn’t equal exactly 17.667 then it won’t fire.
Try <= or >= for these values.

2 Likes

could he try using math.round for the clocktime?

Nope. The script still doesn’t work. The time has passed, and the lanterns remain turned on.
Thats how it looks now:

lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
	if lighting.ClockTime >= 17.667 and lighting.ClockTime <= 7 then
		for i, v in pairs(Lanterns:GetDescendants()) do
			if v:IsA("MeshPart") then
				if v.Material == Enum.Material.Neon then
					v.Transparency = 0
					v.BrickColor = BrickColor.Cork
				end
			end
		end
		for i, v in pairs(HouseLights:GetDescendants()) do
			if v:IsA("Part") then
				if v.Material == Enum.Material.Neon then
					v.BrickColor = BrickColor.Cork
				end
			end
			if v:IsA("PointLight") then
				v.Enabled = true
			end
		end
	elseif lighting.ClockTime >= 7 and lighting.ClockTime <= 17.666 then
		for i, v in pairs(Lanterns:GetDescendants()) do
			if v:IsA("MeshPart") then
				if v.Material == Enum.Material.Neon then
					v.Transparency = 0.5
					v.BrickColor = Color3.fromRGB(0, 0, 0)
				end
			end
		end
		for i, v in pairs(HouseLights:GetDescendants()) do
			if v:IsA("Part") then
				if v.Material == Enum.Material.Neon then
					v.BrickColor = Color3.fromRGB(0, 0, 0)
				end
			end
			if v:IsA("PointLight") then
				v.Enabled = false
			end
		end
	end
end)

Alright. Thank you all guys, I fixed it. After me fixing the :GetPropertyChangedSignal and the >= and <= it gave me an error about BrickColor in RGB like in this lane:

v.BrickColor = Color3.fromRGB(0, 0, 0)

Changed it to:

v.Color = Color3.fromRGB(0, 0, 0)

And everything is working. Thank you!

1 Like