Making Flickering Lights

Hello! I have multiple of the same lamp models in my game and I want all of them to flicker at the same time. I want to make them flicker a bunch of times so since writing every single lamp model separately would be inefficient, I want them in a repeat until command so I could make them repeat the same lines of codes until it reaches it’s goal (that being the NumberValue I’ve added to the folder that contains the models). Using the current script that I’ve made, the code would repeat itself, also adding +1 value to the NumberValue at the same time so when the NumberValue’s Value reaches 10, it would stop flickering. Though the problem with this current script is that from my understanding, it can only change ONE of the models’ properties so that’s why I am having trouble.

By the way, here’s the current code that I’m using and the photo of what the Model Folder contains.

local LightsFlickering = coroutine.create(function() -- I need the coroutine to make the script read this code while it also does something else.
	
	for i, v in pairs(Lamps:GetChildren()) do -- Supposed to get the children of the Folder, that being the Lamp models.
		for i, b in pairs(v:GetChildren()) do -- Supposed to get the children of the models.
			if b:isA("BasePart") then -- Checks if the children are a BasePart.
				if b.Name == "LightBulb" then -- Checks if the target Part is named "LightBulb", which is also the part I have to change it's properties.
					
					repeat -- Changes the properties repeatedly until it reaches the max Value.
						b.Material = "Glass"
						b.Spotlight.Enabled = false
						wait(0.1)
						b.Material = "Neon"
						b.Spotlight.Enabled = true
						Lamps.Counter.Value = Lamps.Counter.Value + 1
						wait(0.1)
						
					until Lamps.Counter.Value == 10
					
					b.Material = "Glass" -- Ends the code by flickering for the last time, being the longest outage.
					b.Spotlight.Enabled = false
					wait(2)
					b.Material = "Neon"
					b.Spotlight.Enabled = true
					
				end
			end
		end
	end
end)

Capture

I would appreciate any help with possibly improving my script or creating an alternate solution. Thank you!

2 Likes

You can wrap the repeat in a coroutine or task.spawn.

Fixed code:

local LightsFlickering = coroutine.create(function()
	for i, v in ipairs(Lamps:GetChildren()) do
		for i, b in ipairs(v:GetChildren()) do
			if b:IsA("BasePart") and b.Name == "LightBulb" then			
				task.spawn(function()
					repeat
						b.Material = Enum.Material.Glass
						b.Spotlight.Enabled = false
						
						task.wait(0.1)
						b.Material = Enum.Material.Neon
						b.Spotlight.Enabled = true
						Lamps.Counter.Value += 1
						
						task.wait(0.1)
						
					until Lamps.Counter.Value == 10

					b.Material = Enum.Material.Glass
					b.Spotlight.Enabled = false
					
					task.wait(2)
					b.Material = Enum.Material.Neon
					b.Spotlight.Enabled = true
				end)
			end
		end
	end
end)
1 Like

Thanks for the script! Although now my problem is that the value increases 6 times instead of 1 every time the repeat happens (probably because it detects all the lamps when the repeat happens and there are 6 of them). Is there a way to fix that and make it only add 1 value every time or would I just have to make the NumberValue limit 60 or something (since I want the loop to happen only 10 times)?

This was a stupid question, 60 would do it.

1 Like

Try this:

local LightsFlickering = coroutine.create(function()
	for i, v in ipairs(Lamps:GetChildren()) do
		for i, b in ipairs(v:GetChildren()) do
			if b:IsA("BasePart") and b.Name == "LightBulb" then
				task.spawn(function()
					local lampCounter = 0
					
					repeat
						b.Material = Enum.Material.Glass
						b.Spotlight.Enabled = false
						
						task.wait(0.1)
						b.Material = Enum.Material.Neon
						b.Spotlight.Enabled = true
						lampCounter += 1
						
						task.wait(0.1)
						
					until lampCounter >= 10

					b.Material = Enum.Material.Glass
					b.Spotlight.Enabled = false
					
					task.wait(2)
					b.Material = Enum.Material.Neon
					b.Spotlight.Enabled = true
				end)
			end
		end
	end
end)
2 Likes

Yeah this does it. I suppose I won’t need the NumberValue since there is a variable to handle it’s job. The previous question was just so it would seem cleaner (this script is much better though) but it was a stupid question anyway lol. Again, thank you for helping!

1 Like

If you wouldn’t mind, I have a lot better question.

Sure, feel free to ask.

Since I’m bad at coding I don’t have that much information on these types of things so I was just curious. When using the; for i, v in ipairs command, is “v” just the name given to the instance by the code itself so it could read it a lot easier? And can I change it to something more recognizable (a custom name for it I mean)?

I see everyone use the “v” for it so I’m unsure if it’ll break anything.

v is just the variable used to reference the object. It’s recommended to change the name to whatever fits best, as v is not very descriptive.

1 Like

For example:

local LightsFlickering = coroutine.create(function()
	for i, child in ipairs(Lamps:GetChildren()) do
		for i, part in ipairs(child:GetChildren()) do
			if part:IsA("BasePart") and part.Name == "LightBulb" then
				task.spawn(function()
					local lampCounter = 0
					
					repeat
						part.Material = Enum.Material.Glass
						part.Spotlight.Enabled = false
						
						task.wait(0.1)
						part.Material = Enum.Material.Neon
						part.Spotlight.Enabled = true
						lampCounter += 1
						
						task.wait(0.1)
						
					until lampCounter >= 10

					part.Material = Enum.Material.Glass
					part.Spotlight.Enabled = false
					
					task.wait(2)
					part.Material = Enum.Material.Neon
					part.Spotlight.Enabled = true
				end)
			end
		end
	end
end)
1 Like

So it should be fine to rename it to whatever from what I can understand. Thank you for teaching me about it. It was stuck in my head for a long time.

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