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