I’m working on a brownout where all lights decreases and turns back to normal in a short time.
for i, v in pairs(workspace.Lights:GetChildren()) do
if v.ClassName == "Model" then
v.Lamp.SurfaceLight.Brightness=0.2
wait(1)
v.Lamp.SurfaceLight.Brightness=2
end
end
the reason why I’m confident about it’s all about wait() is because when I remove string 4 and 5 (wait() and the one that sets .Brightness to 2 back again) it decreases the brightness to 0.2, so the script actually works properly but ignores the 1 second delay and immediately sets the .Brightness to 2 which is not the thing I exactly want.
for i, v in pairs(workspace.Lights:GetChildren()) do
wait(1)
if v.ClassName == "Model" then
v.Lamp.SurfaceLight.Brightness=0.2
v.Lamp.SurfaceLight.Brightness=2
end
end
Nope, still doesn’t work. .Brightness stays at 2 no matter what.
In case if you didn’t get it; I’m trying to make the .Brightness stay at 0.2 for a few seconds, then turn back to 2 (so it basically acts like there’s a voltage drop). Your script seems like it makes sense a little bit, but still won’t work properly.
for i, v in pairs(workspace.Lights:GetChildren()) do
if v.ClassName == "Model" then
task.spawn(function()
v.Lamp.SurfaceLight.Brightness=0.2
task.wait(1)
v.Lamp.SurfaceLight.Brightness=2
end
end
end
Use task.wait() for new work in favour of wait(), assuming you’re trying to make all lights dim you can use task.spawn() to run the code in a new thread (won’t pause the code for other lights)
I think it’s because the script’s setting one light’s brightness to 0.2, waiting 1 second, then setting it back to 2, and then moving on to the next light. You could try using coroutines:
for i, v in pairs(workspace.Lights:GetChildren()) do
if v.ClassName == "Model" then
coroutine.wrap(function()
v.Lamp.SurfaceLight.Brightness=0.2
wait(1)
v.Lamp.SurfaceLight.Brightness=2
end)()
end
end
AFAIK they behave and perform identically, but coroutine.wrap returns a coroutine which you need to run (hence the “()” at the end of coroutine.wrap example I gave) while task.spawn runs it instantly. I haven’t heard of any performance benefits, the only real difference is the ().
local lights = workspace.lights
for _, light in ipairs(lights:GetDescendants()) do
if light:IsA("Light") then
light.Brightness = 0.2
task.wait(1)
light.Brightness = 2
end
end
Made a lot friendlier/easier for the younger developers that Roblox attracts. They serve a lot less functionality than that which is possible with coroutines.