What you're looking for, is a while or repeat loop. Now, while and repeat loops work the same way, in terms of running code over and over again until a false or nil condition is met.
While vs Repeat?
One difference between the while and repeat loop, is the order in which they run. The while loop will check it's condition before running the code to decide whether or not to continue (exactly like an if statement), and the repeat loop will check it's condition after it's already ran it's code, then depending on that condition, will determine whether or not to continue.
Another difference between these two loops, is that the while loop will continue to run it's code until it's condition is false, and the repeat loop will continue to run it's code until it's condition is true. So their pretty much an inverse of each other.
So basically, the while loop is pretty much a repeat loop with an if statement at the beginning (obviously excluding their syntax differences)
Which to use?
In most cases, it won't matter which loop you use. The while loop is a bit more popular, but not because it runs any faster or more efficiently than the repeat loop. It just seems to be more practical in most cases. But for this example, I'll demonstrate how to use both with your question.
Alternative setup
Before I continue with the loop examples, I'd like to point out one other thing you may find handy. Instead of creating a separate variable for each light, you could have function in which you can use to return every single light you need, for quick reference. Here's an example:
local Model = workspace:WaitForChild("Model")
local function GetLights(Model)
local Lights = {}
local function Add(x)
for i,v in next, x:GetChildren() do
if v:IsA("PointLight") then
Lights[#Lights+1] = v
end
Add(v)
end
end
Add(Model)
return Lights
end
While loop
Now let's actually implement the while loop in our revised code:
local Model = workspace:WaitForChild("Model")
local Lights
local function GetLights(Model)
local Lights = {}
local function Add(x)
for i,v in next, x:GetChildren() do
if v:IsA("PointLight") then
Lights[#Lights+1] = v
end
Add(v)
end
end
Add(Model)
return Lights
end
local function ToggleLights()
if type(Lights) == "table" then
for i,v in next, Lights do
v.Enabled = not v.Enabled
end
end
end
Lights = GetLights(Model)
while wait(0.5) do
ToggleLights()
wait(0.5)
ToggleLights()
end
Repeat loop
Using the repeat loop, we can do the exact same thing, just with a different syntax and keeping in mind the code will run BEFORE it checks the condition
local Model = workspace:WaitForChild("Model")
local Lights
local function GetLights(Model)
local Lights = {}
local function Add(x)
for i,v in next, x:GetChildren() do
if v:IsA("PointLight") then
Lights[#Lights+1] = v
end
Add(v)
end
end
Add(Model)
return Lights
end
local function ToggleLights()
if type(Lights) == "table" then
for i,v in next, Lights do
v.Enabled = not v.Enabled
end
end
end
Lights = GetLights(Model)
repeat wait(0.5)
ToggleLights()
wait(0.5)
ToggleLights()
until nil
And that's about it. If you have any questions, just let me know.
1 comment
I think I worded my question wrong, using the same code how could I obtain a similar effect to that of siren's on a police car? Back and fourth alternating between colors. Thanks in advanced!