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

How do I minimize this loop to be as small as possible?

Asked by
baxzzi 42
4 years ago

I'm trying to run a loop for my traffic lights, all works well because it's really messy. I want to figure out how clean the crosswalk signal loop up by running a separate loop inside of the main loop for a certain amount of times.

local startRed = script.Parent.redFULL
local startGreen = script.Parent.greenARROW
local crossWalk1red = script.Parent.crossWalk_Red1
local crossWalk1white = script.Parent.crossWalk_White1
local crossWalk2red = script.Parent.crossWalk_Red2
local crossWalk2white = script.Parent.crossWalk_White2

while true do
    startGreen.red.Transparency = 0.95
    wait(0.1)
    startGreen.green.Transparency = 0
    wait(13)     --Time the GreenLight stays on
    startGreen.green.Transparency = 0.95
    wait(0.1) -- Interval Between changing to different Light
    startGreen.yellow.Transparency = 0
    wait(2) --Time the YellowLight stays on
    startGreen.yellow.Transparency = 0.95
    wait(0.1) -- Interval Between changing to different Light
    startGreen.red.Transparency = 0

    wait(1)

    startRed.red.Transparency = .95
    crossWalk2red.light.Color = Color3.fromRGB(17, 17, 17) --red off
    wait(0.1)
    startRed.green.Transparency = 0
    crossWalk2white.light.Color = Color3.fromRGB(248, 248, 248) --white on
    wait(6.5)
    crossWalk2white.light.Color = Color3.fromRGB(17, 17, 17) --white off
    wait(0.1)
    crossWalk2red.light.Color = Color3.fromRGB(255, 0, 0) -- red on
    wait(0.5)
    crossWalk2red.light.Color = Color3.fromRGB(17, 17, 17) --red off
    wait(0.5)
    crossWalk2red.light.Color = Color3.fromRGB(255, 0, 0) -- red on
    wait(0.5)
    crossWalk2red.light.Color = Color3.fromRGB(17, 17, 17) --red off
    wait(0.5)
    crossWalk2red.light.Color = Color3.fromRGB(255, 0, 0) -- red on
    wait(0.5)
    crossWalk2red.light.Color = Color3.fromRGB(17, 17, 17) --red off
    wait(0.5)
    crossWalk2red.light.Color = Color3.fromRGB(255, 0, 0) -- red on
    wait(0.5)
    crossWalk2red.light.Color = Color3.fromRGB(17, 17, 17) --red off
    wait(0.5)
    crossWalk2red.light.Color = Color3.fromRGB(255, 0, 0) -- red on
    wait(0.5)
    crossWalk2red.light.Color = Color3.fromRGB(17, 17, 17) --red off
    wait(0.5)
    crossWalk2red.light.Color = Color3.fromRGB(255, 0, 0) -- red on
    wait(0.5)
    crossWalk2red.light.Color = Color3.fromRGB(17, 17, 17) --red off
    wait(0.5)
    crossWalk2red.light.Color = Color3.fromRGB(255, 0, 0) -- red on
    startRed.green.Transparency = 0.95
    wait(0.1)
    startRed.yellow.Transparency = 0
    wait(2)
    startRed.yellow.Transparency = 0.95
    wait(0.1)
    startRed.red.Transparency = 0
    wait(1)
end
0
I had a feeling this question would get a ton of upvotes greatneil80 2647 — 4y
0
Especially since this is a copy of a same question which got 11 answers and 20+ upvotes in a week... greatneil80 2647 — 4y

3 answers

Log in to vote
8
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

You can separate your code into functions / loops to make it cleaner.

local startRed = script.Parent.redFULL
local startGreen = script.Parent.greenARROW
local crossWalk1red = script.Parent.crossWalk_Red1
local crossWalk1white = script.Parent.crossWalk_White1
local crossWalk2red = script.Parent.crossWalk_Red2
local crossWalk2white = script.Parent.crossWalk_White2

--Turns a light on for totalTime seconds then turns it off.
function switchLight(lightModel, totalTime)
    lightModel.Transparency = 0
    wait(totalTime)
    lightModel.Transparency = .95
end

--Makes a light blink numTimes 
function makeLightBlink(lightModel, numTimes)
    for i = 1, numTimes do
        lightModel.Color = Color3.fromRGB(255, 0, 0) -- red on
        wait(0.5)
        lightModel.light.Color = Color3.fromRGB(17, 17, 17) --red off
        wait(0.5)
    end
end

while true do
    switchLight(startGreen.green, 13)
    switchLight(startGreen.yellow, 2)
    switchLight(startGreen.red, 1)
    makeLightBlink(crossWalk2red.light, 10)
end
0
You split the number of lines in half greatneil80 2647 — 4y
0
Oh, I was expecting something way different but this is even better. Thank you :) baxzzi 42 — 4y
0
any time. royaltoe 5144 — 4y
Ad
Log in to vote
0
Answered by
Kblow1 53
4 years ago

Well, for the Cross walk you could make it a for loop

for i=0,13 do
    crossWalk2red.light.Color = Color3.fromRGB(255, 0, 0) -- red on
    wait(0.5)
    crossWalk2red.light.Color = Color3.fromRGB(17, 17, 17) --red off
end
0
you missed the wait(.5) on line 4 greatneil80 2647 — 4y
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

easy, variables. always.

make a variable for the colors (green, yellow, red). make a variable for the light of each light. make variables for the transparency (off = .95, on = 0. this won't necessarily make it shorter but it would make it more readable).

one other thing you can do is change the whole "red off, red on" to something like this:

local loops = 9 -- change this if you want
local red = Color3.fromRGB(255, 0, 0)
local CRed = Color3.fromRGB(17, 17, 17)
for i=1,loops,1 do
    crossWalk2red.light.Color =  i%== 0 and red or CRed -- red on
    wait(0.5)
end
1
Using functions is gonna make it so much simpler like lucyyy said. you can also use the loop i wrote here to achieve the same behaviour with functions for much simpler code GGRBXLuaGG 417 — 4y

Answer this question