Script wont disable light with night/day cycle

Hello. i have this script where my parts material should turn plastic in the day. and turn neon in the night.
i have a issue with that since it wont do anything.
here is my code:

local Lighting = game:GetService("Lighting")
local Minutes = 0

function Lights(boolvalue)
	if boolvalue == true then
		for _, v in pairs(workspace.Lights.Lantern2:GetChildren()) do
			if "LightPart" then
				workspace.Lights.Lantern2.LightPart.Material = Enum.Material.Neon
			elseif boolvalue == false then
				for _, v in pairs(workspace.Lights.Lantern2:GetChildren()) do
					if "LightPart" then
						workspace.Lights.Lantern2.LightPart.Material = Enum.Material.Plastic
					end
				end
			
			end
			
	
		
			
			
		end
	end
end

Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
	if Lighting.ClockTime >= 18.5 or Lighting.ClockTime <= 7 then
		Lights(true)
	elseif Lighting.ClockTime >= 7 or Lighting.ClockTime <= 18.5 then
		Lights(false)
	end
end)

while true do
	Minutes += 1
	Lighting:SetMinutesAfterMidnight(Minutes)
	task.wait(1 / 50)
end
1 Like

Hello! A few things may help you; firstly, in your Lighting:GetPropertyChangedSignal() function, your initial if Lighting.ClockTime statement only needs to check before 7 or after 18.5 (6:30pm), not at the exact time, that can be used in the following else. I also recommend adding a debounce to only run the function once when the time is checked.

Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
	if Lighting.ClockTime > 18 or Lighting.ClockTime < 6 then --This only checks if it's before 6am or after 6pm
		if lightsActivated == false then
			lightsActivated = true --debounce
			Lights(true) --This should only have to run once because we just inverted the debounce boolValue
		end
	elseif Lighting.ClockTime >= 6 and Lighting.ClockTime <= 18 then --It is either or between 6am and 6pm
		if lightsActivated == true then
			lightsActivated = false
			Lights(false)
		end
	end
end)

In your Lights(boolvalue) function, in the for loop you have if "LightPart" then, I assume looking for the name of the bulb? This can better be achieved using :GetDescendants() on Lights and looking for v.Name to match “LightPart”.

for _, v in pairs(workspace.Lights:GetDescendants()) do
	if v.Name == "LightPart" then
		v.Material = Enum.Material.Plastic
	end
end
Updated Script
local Lighting = game:GetService("Lighting")
local Minutes = 0

local lightsActivated = false --debounce

function Lights(lightsOn)
	if lightsOn == true then
		print("Lights have turned on")
		for _, v in pairs(workspace.Lights:GetDescendants()) do
			if v.Name == "LightPart" and v:FindFirstChildOfClass("PointLight") then
				v:FindFirstChildOfClass("PointLight").Enabled = true
				v.Material = Enum.Material.Neon
			end
		end
	else --lightsOn == false
		print("Lights have turned off")
		for _, v in pairs(workspace.Lights:GetDescendants()) do
			if v.Name == "LightPart" and v:FindFirstChildOfClass("PointLight") then
				v:FindFirstChildOfClass("PointLight").Enabled = false
				v.Material = Enum.Material.Plastic
			end
		end
	end
end

Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
	if Lighting.ClockTime > 18 or Lighting.ClockTime < 6 then --This only checks if it's before 6am OR after 6pm
		if lightsActivated == false then
			lightsActivated = true --debounce
			Lights(true) --This should only have to run once because we just inverted the debounce boolValue
		end
	elseif Lighting.ClockTime >= 6 and Lighting.ClockTime <= 18 then --It is either or between 6am AND 6pm
		if lightsActivated == true then
			lightsActivated = false
			Lights(false)
		end
	end
end)

while task.wait(1 / 50) do --0.02
	Minutes += 1
	Lighting:SetMinutesAfterMidnight(Minutes)
end
Video of script in action

Roblox Studio Light Toggle Script - YouTube

Hope this helps! :smiley:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.