[Solved] Light scheduling help

Hello,

I’m trying to set up a system that sets all of the chosen lights in my world to enabled at certain times, and to do this I am using the CollectionService. The reason being that I do not want to have one script per light, of which I will have many- as I worry it will effect the performance of my game.

This is my most recent attempt. This code is found in a standard script under ServerScriptService:

local CollectionService = game:GetService("CollectionService")
local Lighting = game:GetService("Lighting")

while task.wait() do
	for _,V in pairs(CollectionService:GetTagged("Light")) do
		if Lighting.ClockTime < 6 or Lighting.ClockTime > 16 then
			V.Enabled = true
		else
			V.Enabled = false
		end
	end
end

I have attempted doing effectively the same within module scripts too, though I was again unsuccessful. If anyone could help me out, it’d be greatly appreciated.

Thanks

How/where did you add a tag for all Lights?

I’m using a plugin called Tag Editor to add a tag to the part with the light.

Can you try:

print(CollectionService:GetTagged("Light"))

And show me the output.

How is the clocktime changed? via script or are you just changing it in studio

oh wait…

You’re checking the lighting.ClockTime inside of the for loop.

local CollectionService = game:GetService("CollectionService")
local Lighting = game:GetService("Lighting")

while true do
    if Lighting.ClockTime < 6 or Lighting.ClockTime > 16 then
        for _,V in pairs(CollectionService:GetTagged("Light")) do
            V.Enabled = true
        end
    else
        for _,V in pairs(CollectionService:GetTagged("Light")) do
            V.Enabled = false
        end
    end
    task.wait()
end

This is how I would do it.

(I could be wrong and I am open for improvements)

His method would still work although a better way to write your way would be

while true do
    local LightState = Lighting.ClockTime < 6 or Lighting.ClockTime > 16
    for _,V in pairs(CollectionService:GetTagged("Light")) do
        V.Enabled = LightState
    end
    task.wait()
end

Here:
image

I am just changing it in studio.

You might be changing it locally if your changing it via studio be sure your changing it on the server, you can print the clocktime(in a server script) to be sure

It’s working perfectly now, this might have been the case. Thank you (and @Tekakato2) very much!

2 Likes