Suggestions for a blackout

I want to make a blackout script which would turn off all the lights in my game, by looping through all the light objects in my game. But, I can’t just have all my lights have the same name and loop through workspace to find them to set a color to v, because all my lights will have different colors or different brightness in blackout mode. What do you suggest I should use, putting a color3value + string value in all my lights so I can just loop through the game searching for the value and if it does exist, set the brightness or color to that part, OR, use a custom attribute and run through all my objects in my game using a pcall to see if that property exists and if it does set it to the part. I cannot use Collection service because each part I tag will have a different light property set to it. So which should I use, use a color3value+string, use a attribute, or a entirely different method. I want to keep the least stress on the server and keep it simple for me as well.

1 Like

How many lights are there?
You could just move all the light parts in a folder somewhere in the workspace and loop through them, or add them all in an array and loop through the array.
Something like this:

local lights = {
--position of light1,
--position of light2,
--position of light3
}
for i, light in ipairs(lights) do
light.blackout.Value = false
end

potentially tens of thousands. I want to keep the parts in the model they’re in, it would be a pain to put it to a folder and I would want to edit that light I would not be able to, I would have to search the folder through a sea of lights. Also, I just now changed my mind of the blackout value, I originally wanted it so if I turned a bool value called “blackout” (inside of replicatedstorage) to true, it would turn off all the lights, but that would mean all my lights would have to be listening for it to turn to true or false, so I’d rather be looping through workspace to find the light object. So, back to my original question, to turn off the lights in my game, should I have a color3value + a int value, so I can loop through workspace looking for those objects, and simply copy the value of them and paste it into the property of the light, or instead use a custom attribute, looping through all the objects in my game checking if a part has that property using a pcall, if it does then paste that property value into the color property, or use collection service tags to achieve the same effect.

I suggest to make a loop that starts when the server starts and loops through all the parts in game to check whether they are lights. Then put those in an array, which you can then read from whenever you want. And then use attributes to save how you want them to look. Something like this:

local lights = {}
for i,v in pairs(workspace:GetDescendants()) do --run this only once when the script loads
   if v:IsA('PointLight') or v:IsA('SurfaceLight') or v:IsA('SpotLight') then
      table.insert(lights,v)
   end
end

for i,v in pairs(lights) do --run this whenever you want to start the blackout
  v.color = v:GetAttribute('blackoutcolor') --etc. etc.
end
1 Like

Technically, assuming you can’t use CollectionService, you can have a lights array containing all the lights, inside a single script(or replicate it if needed, for example with _G), then only loop through the game once, and then just listen for changes:

local lights = {}

function addLight(object)
	if object:GetAttribute("Light") then --assuming you chose the attribute solution
		table.insert(lights, object)
	end
end

for _, object in pairs(workspace:GetDescendants()) do 
	addLight(object)
end 

workspace.DescendantAdded:Connect(addLight)
workspace.DescendantRemoving:Connect(function(object)
	local index = table.find(lights, object)
	if index then 
		table.remove(lights, index)
	end
end)

for _, light in pairs(lights) do 
	--do something with the current in-game lights
end

I cannot do this because each of my lights will have different properties light properties. I cannot just loop through all the lights in the game and give the light the same property as all the other ones.