Lights Not Deactivating

Okay, so I’m trying to make a game with random events, one of them being all the lights go out for a short period of time. Now, all sounds work, and all the stuff prints, but the lights don’t actually change color. I’d also like them to actually turn off the brightness, or better yet disable the lights in the parts, but I haven’t gotten to that part yet.

For those of you wondering, the lights are parts, with point lights inside of them. They are all grouped together into one model

local sound = script:WaitForChild("LightsOff")
local sound2 = game.Workspace["Ambiance"]

while true do
	task.wait(5)
	local random = math.random(1,6)
	if random == 6 then
		print("Lights Out")
		sound:Play()
		sound2:Stop()
		for key, value in pairs(workspace.Lights:GetDescendants()) do
			if value.Name == "Light" then
				value.BrickColor = BrickColor.new("Really black")
		task.wait(30)
			sound2:Play()
				print("Event Over")
			end
		end
	end
end
1 Like

Any error messages in console?

Usually when I’m not getting an error with a loop and it’s not doing what it should be, the first thing to check is to see if the loop is actually iterating through anything.

In your case, you should verify that the code inside the loop is actually executing. You could print the key each time the loop iterates for example, or even check how long the table is you are getting from GetDescendants. Perhaps the mistake is when you get the lights in this line: for key, value in pairs(workspace.Lights:GetDescendants()) do

Nope. Not a single error message in the output or console

I have it print “Lights Out” everytime the event occurs. It simply doesn’t edit the lights.

task.wait(5)
local random = math.random(1,6)
if random == 6 then

Is it possible that you’re not waiting enough time? This condition (on average) will be met every 30 seconds.

Like I said, the event doesn’t actually have any trouble starting up. It’s simply the lights. They do not change despite the code in the script.

Right, but is it also printing “Event Over”?

Yes, it prints the start and the end.

Edit: I’ve seen in the past that editing children of a model is trippy.

No, they’re asking if print("Event Over") executes. If it isn’t then this if value.Name == "Light" then conditional isn’t being satisfied, and you need to double check the names of your instances.

It prints both messages that it is supposed to. It prints “Lights Out”, and then it prints “Event Over”

https://developer.roblox.com/en-us/api-reference/class/PointLight

PointLights don’t have a BrickColor property.

Light.Color = Color3.new(0, 0, 0)

If you want them to turn black.

Thats for the parts. Lights is something else. I’m trying to get the part color to change

I’ll leave this one with you, best of luck.

1 Like

If the loop is printing “Event Over” for each light, then it’s probably the code inside the loop. I also noticed you are waiting 30 seconds between each light… which is fine if you only have 1 light, but if you have more than that, you’re going to be waiting a while to see it change over. How many lights do you have?

So if I’m understanding this right, the “Lights” are actually just bricks and not some type of light object?

Yes. They do have pointLights in the parts, but they aren’t my main worry. To answer the previous part:
Well, the 30 seconds is the length of the event. All of the lights go dark instantly, then the event ends after 30 seconds. I have probably close to 3k “lights” in the entire map.

Put it simply, I’m trying to select all the children of a model, then trying to change their color.

Oh, I see. I think I know what’s going on here. I think this is what you want…

local sound = script:WaitForChild("LightsOff")
local sound2 = game.Workspace["Ambiance"]

local objects_per_batch = 20
-- This can be adjusted based on your performance needs. Higher means more parts processed for every ~0.03 seconds

while true do
	task.wait(5)
	local random = math.random(1,6)
	if random == 6 then
		print("Lights Out")
		sound:Play()
		sound2:Stop()
		for key, value in pairs(workspace.Lights:GetDescendants()) do
                        -- For each descendant…
			if value.Name == "Light" then
                          -- For each light, which should execute about 3,000 times based on your description
				value.BrickColor = BrickColor.new("Really black")
                        
			end
            if key % objects_per_batch == 0 then
                task.wait()
            end
        end
        -- This will execute once all the lights change color
        sound2:Play()
        task.wait(30)
        print("Event Over")
	end
end

You were waiting 30 seconds for each light, instead of waiting 30 seconds once all the lights were off/change color. It appeared as if nothing was happening because in the first 30 seconds, only one of 3,000 lights changed color, and the code was just sitting there waiting while you were thinking “Oh, that didn’t change anything.” Hopefully this fixed it and makes more sense now!

Oh thanks! This works perfectly, and the pause it takes adds to the effect. Just a side question, how would I temporarily disable the actual pointLights?

Edit: Also, just playtested a little longer, and it seems the lights don’t come back on after the 30 seconds. I forgot to add I needed that.

1 Like