How would I do this?

So I have a loop which makes lights flicker.
The lights are inside of a folder and a for loop is used to get the lights, however the problem is that the lights all flicker at the same time, so how could I make the lights flicker at different times?

CODE:

local function FlickerLight()
	workspace.FlickerLoop:Play()
	while CanFlicker do
		local RandomInterval = math.random(.1, 1)
	for i, v in pairs(LightsFolder) do
		if v:IsA("SurfaceLight") or v:IsA("SpotLight") then
			local Light = v
			--print(v)
			local LightEffect = Light.Parent
			--print(Light)
			--print(LightEffect:GetChildren())
			local FlickerLoopSound = LightEffect:FindFirstChild("LightFlickerLoop")
			local FlickerSound = LightEffect:FindFirstChild("LightFlicker")
			local FlickerSound2 = LightEffect:FindFirstChild("LightFlicker1")
			--print(FlickerLoopSound)

				 coroutine.wrap(function()
				--FlickerLoopSound.Playing = true
				local r = math.random(1,2)
				if r == 1 then
					--FlickerSound:Play()
				else
					--FlickerSound2:Play()
				end
				 Light.Enabled = false
				 LightEffect.Material = Enum.Material.Plastic
				-- print('disabled')
				 task.wait(.5)
				-- print('enabled')
				 Light.Enabled = true
				 LightEffect.Material = Enum.Material.Neon
				end)()
			end
			
			end
	task.wait(.1)
	end
end

FlickerLight()
2 Likes

Maybe you can try making a table and store all of the lights into that, and then you can pick out of the table a random light, or more by using math.random on the table.

2 Likes

how would I make a table of the lights? the stuff in the Lights folder is like this:
Workspace>LightsFolder>(bunch of models that contain lights)>Actual Light

like this?

local Lights = {how would i put the lights here, using get descendants and if the descendants = a light?}
while true do
local RandomLight = Lights[math.random(1, #Lights)]
--flickering stuff
end

(using table.insert in a for loop?)

1 Like

Get Descendants gets every child from the Instance requested, so defiantly don’t use that for this, if your using Point Light Instances or anything.

You can do a for i v loop through the lights folder, and add every light into there.

if you wanna add something to a table, you can also just use

tableName[Instance] = Instance.Name

They would all probably be named something like Light, but when you do loop for something random, it will have a table value (TableVal1, TableVal2)

1 Like