Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

I created two sets of lights that I'd like to loop constantly, how would I go about doing so?

Asked by
sethre 0
8 years ago

This is the code in question:

local greenLight = game.Workspace.GreenLight.SurfaceLight
local redLight = game.Workspace.RedLight.SurfaceLight
local greenLight1 = game.Workspace.GreenLight1.SurfaceLight
local redLight1 = game.Workspace.RedLight1.SurfaceLight

wait(5)

greenLight.Enabled = true
redLight.Enabled = false
greenLight1.Enabled = true
redLight1.Enabled = false

wait(.5)

greenLight1.Enabled = false
redLight1.Enabled = true
greenLight.Enabled = false
redLight.Enabled = true

What would I need to do to make it permanently alternate/repeat itself so the light has an alternating strobe effect?

2 answers

Log in to vote
2
Answered by 8 years ago

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:

-- Let's just say the name is model, and this is where all the lights are.
local Model = workspace:WaitForChild("Model")

-- Function that returns a table (array style) of lights inside each part within the model
-- This is just a recursive function used to search all objects inside a model to return all PointLight objects within it. Though, explaining how all of this works would be for another question.

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

-- A function to check the "Lights" table and invert their Enabled property.
local function ToggleLights()
    if type(Lights) == "table" then -- If it's a table
        for i,v in next, Lights do
            v.Enabled = not v.Enabled -- Invert their Enabled property
        end
    end
end

-- "Lights" is now an array containing all PointLight objects within the model.
Lights = GetLights(Model)

-- The while loop will only run if it's condition between the "while" and "do" statements is true. If it's not true, the code inside the while loop will not run. This is why using "while true do" is used to create infinite loops. In this case, we can say while wait(0.5) do, since the number wait() returns is considered true.
while wait(0.5) do
    ToggleLights() -- Turn them all on/off
    wait(0.5)
    ToggleLights() -- Turn them all off/on
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 -- If it's a table
        for i,v in next, Lights do
            v.Enabled = not v.Enabled -- Invert their Enabled property
        end
    end
end

Lights = GetLights(Model)

repeat wait(0.5)
    ToggleLights() -- Turn them all on/off
    wait(0.5)
    ToggleLights() -- Turn them all off/on
until nil -- Repeat forever

And that's about it. If you have any questions, just let me know.

0
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! sethre 0 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

Maybe you wanna learn what loops are. try to watch these tutorials..

https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=4&cad=rja&uact=8&ved=0ahUKEwjP7d7IgtHLAhVGC44KHTwECrgQtwIIOjAD&url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DsCCh37muLew&usg=AFQjCNFbuw2nTn8qU9FaHmbveX-eNd3YzA&sig2=H--8TI9KH_BsZzK_I13ADQ

http://wiki.roblox.com/index.php?title=Loops

solution would be

local greenLight = game.Workspace.GreenLight.SurfaceLight
local redLight = game.Workspace.RedLight.SurfaceLight
local greenLight1 = game.Workspace.GreenLight1.SurfaceLight
local redLight1 = game.Workspace.RedLight1.SurfaceLight

while wait(5) do

    greenLight.Enabled = true
    redLight.Enabled = false
    greenLight1.Enabled = true
    redLight1.Enabled = false

    wait(.5)

    greenLight1.Enabled = false
    redLight1.Enabled = true
    greenLight.Enabled = false
    redLight.Enabled = true

end

Hope that helps ya!

Answer this question