Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

2 comments

0

I had a feeling this question would get a ton of upvotes

0

Especially since this is a copy of a same question which got 11 answers and 20+ upvotes in a week...

3 answers

✓ Accepted answer

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

3 comments

0

You split the number of lines in half

0

Oh, I was expecting something way different but this is even better. Thank you :)

0

any time.

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

1 comment

0

you missed the wait(.5) on line 4

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 comment

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

This discussion is preserved as it was. New answers, comments, and votes are closed.