Script ignores Wait() inside the "for" loop

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.

1 Like
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

needa put it outside the if statement

1 Like

Yeah, like S0MBRX said.
Cuz if the Wait(1) is inside the If statement it will only wait 1 second if the if statement meets its requirements.

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)

1 Like

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
1 Like

I completely forgot about task.wait(), thanks.

And yes I was trying to make all lights dim, it works properly now.

You should use task.spawn() for new work, they are more efficient than wrapping with coroutine

1 Like

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

hmmm i recalled them being more performant, anyway thanks for the info

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

Just a slightly alternate approach.

1 Like

Roblox’s task library functions are essentially watered down versions of the functions provided by the coroutine library which is native to Lua.

What do you mean by “watered down”?

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.