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)
I would appreciate any help with possibly improving my script or creating an alternate solution. Thank you!
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)
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)?
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)
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!
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.
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)