Script goes through single light then waits and repeats

I have been coming across this issue recently: Whenever i request the lights to be flickered, the module script goes through only one light and then repeats the process to every single light. But i want to flicker all of them at the same time. How do i fix this?
My current code:

LightFlicker = function()
			for _,v in pairs (workspace.Facility.Lights:GetChildren()) do
				for _,c in pairs(v:GetChildren()) do
					if c:IsA("UnionOperation") and c.Name == "Light" then
						local clone = script.light_flicker_04:Clone()
						clone.Parent = c
						clone:Play()
						c.Color = Color3.fromRGB(0, 0, 0)
						c.SurfaceLight.Color = Color3.fromRGB(0, 0, 0)
						task.wait(.1)
						c.Color = Color3.fromRGB(255, 255, 127)
						c.SurfaceLight.Color = Color3.fromRGB(255, 255, 127)
						task.wait(.1)
						c.Color = Color3.fromRGB(0, 0, 0)
						c.SurfaceLight.Color = Color3.fromRGB(0, 0, 0)
						task.wait(.1)
						c.Color = Color3.fromRGB(255, 255, 127)
						c.SurfaceLight.Color = Color3.fromRGB(255, 255, 127)
						task.wait(.1)
						c.Color = Color3.fromRGB(0, 0, 0)
						c.SurfaceLight.Color = Color3.fromRGB(0, 0, 0)
						task.wait(.1)
						c.Color = Color3.fromRGB(255, 255, 127)
						c.SurfaceLight.Color = Color3.fromRGB(255, 255, 127)
						task.wait(.1)
						c.Color = Color3.fromRGB(0, 0, 0)
						c.SurfaceLight.Color = Color3.fromRGB(0, 0, 0)
						task.wait(.1)
						c.Color = Color3.fromRGB(255, 255, 127)
						c.SurfaceLight.Color = Color3.fromRGB(255, 255, 127)
						task.wait(.1)
						c.Color = Color3.fromRGB(0, 0, 0)
						c.SurfaceLight.Color = Color3.fromRGB(0, 0, 0)
						task.wait(.1)
						c.Color = Color3.fromRGB(255, 255, 127)
						c.SurfaceLight.Color = Color3.fromRGB(255, 255, 127)
					end
				end
			end
		end

The problem is that programming languages run code line by line. In this case Lua has to go through and flicker each light one after another. Fortunately you can use task.spawn() to create a thread that will run without blocking the execution of your main script. You will have to create a function that flickers a certain light and then pass said function to task.spawn() for each light that you want to flicker.

Can you show me an example? I’m sorta confused here.

For example your script could look like this with an implementation of task.spawn():

LightFlicker = function()
			for _,v in pairs (workspace.Facility.Lights:GetChildren()) do
				for _,c in pairs(v:GetChildren()) do
					if c:IsA("UnionOperation") and c.Name == "Light" then
						--task.spawn(flickerLight, c)
					end
				end
			end
		end

Instead of directly flickering the light you create a separate thread to do the flickering to prevent your main script from pausing. The flickerLight(c) function should basically just contain the code required to flicker the light.

Thanks, it works now! :+1: Thanks!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.