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
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
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
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