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.
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
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
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