Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

My lights won't turn on at night, what am I doing wrong?

I'm making some outdoor lights, and they turn off just fine during the day, but I have no idea how to fix my script to make them turn off at night. I am using collectionservice to turn them on and off. This is all of my script since I'm not sure what part is going wrong. There is nothing coming up in the output log, either.

local CollectionService = game:GetService("CollectionService")
local taggedparts = CollectionService:GetTagged("Outdoor Lights")
local clocktime = game.Lighting.ClockTime
local light1 = game.Workspace.Bridge.LightPost1.Lamp.Light.PointLight
local light2 = game.Workspace.Bridge.LightPost2.Lamp.Light.PointLight
local light3 = game.Workspace.Bridge.LightPost3.Lamp.Light.PointLight
local light4 = game.Workspace.Bridge.LightPost4.Lamp.Light.PointLight

for _, taggedparts in pairs(taggedparts) do
	
	if clocktime >= 8 and clocktime <= 18 then
		light1.Enabled = false
		light2.Enabled = false
		light3.Enabled = false
		light4.Enabled = false
	elseif clocktime >= 18 and clocktime <= 8 then
		print("Nighttime")
		light1.Enabled = true
		light2.Enabled = true
		light3.Enabled = true
		light4.Enabled = true
	else
		
	end
	
end

Thank you!

1 answer

✓ Accepted answer

You're only checking for the ClockTime once! So to fix this you'd want to do this.

local CollectionService = game:GetService("CollectionService")
local taggedparts = CollectionService:GetTagged("Outdoor Lights")
local clocktime = game.Lighting.ClockTime
local light1 = game.Workspace.Bridge.LightPost1.Lamp.Light.PointLight
local light2 = game.Workspace.Bridge.LightPost2.Lamp.Light.PointLight
local light3 = game.Workspace.Bridge.LightPost3.Lamp.Light.PointLight
local light4 = game.Workspace.Bridge.LightPost4.Lamp.Light.PointLight

while wait() do
	for _, part in pairs(taggedparts) do
	    if clocktime >= 8 and clocktime <= 18 then
	        	light1.Enabled = false
	        	light2.Enabled = false
	        	light3.Enabled = false
	        	light4.Enabled = false
	    elseif clocktime >= 18 and clocktime <= 8 then
	       	 print("Nighttime")
	       	 light1.Enabled = true
	        	light2.Enabled = true
	        	light3.Enabled = true
	       	 light4.Enabled = true
	    end
	end
end

I'm going to assume there are also lights in the table "taggedparts" so you'd want to also turn on/off the lights in there as well using an If statement in the for loop to search for other lights.Since I don't know how the instances in "taggedparts" are set up, I'm going to assume.

local CollectionService = game:GetService("CollectionService")
local taggedparts = CollectionService:GetTagged("Outdoor Lights")
local clocktime = game.Lighting.ClockTime
local light1 = game.Workspace.Bridge.LightPost1.Lamp.Light.PointLight
local light2 = game.Workspace.Bridge.LightPost2.Lamp.Light.PointLight
local light3 = game.Workspace.Bridge.LightPost3.Lamp.Light.PointLight
local light4 = game.Workspace.Bridge.LightPost4.Lamp.Light.PointLight

while wait() do
	for _, part in pairs(taggedparts) do
		if part:FindFirstChild("PointLight") then
			    if clocktime >= 8 and clocktime <= 18 then
			        light1.Enabled = false
			        light2.Enabled = false
			        light3.Enabled = false
			        light4.Enabled = false
				part.PointLight.Enabled = false
			    elseif clocktime >= 18 and clocktime <= 8 then
			        print("Nighttime")
			        light1.Enabled = true
			        light2.Enabled = true
			        light3.Enabled = true
			        light4.Enabled = true
				part.PointLight.Enabled = true
			end
		end
	end
end

Keep in mind, if there is nothing in "taggedparts" the code in the for loop will not run!! I hope I helped!

1 comment

0

Thank you so much! Turns out I was checking the time wrong as well as this issue, so thank you for pointing it out for me c:

This discussion is preserved as it was. New answers, comments, and votes are closed.