Shortening light pattern script

I have made a light pattern script for a neon sign in the game, but it’s really overcomplicated and I want to know how I can shorten it down.

robloxapp-20230625-1919162.wmv (255.4 KB)

local l1 = script.Parent.Parent.L1;
local l2 = script.Parent.Parent.L2;
local l3 = script.Parent.Parent.L3;
local l4 = script.Parent.Parent.L4;
local l5 = script.Parent.Parent.L5;
local l6 = script.Parent.Parent.L6;

while true do
	
	l1.BrickColor = BrickColor.new("Steel blue");
	l2.BrickColor = BrickColor.new("Steel blue");
	l3.BrickColor = BrickColor.new("Steel blue");
	l4.BrickColor = BrickColor.new("Steel blue");
	l5.BrickColor = BrickColor.new("Steel blue");
	l6.BrickColor = BrickColor.new("Steel blue");
	wait(0.5);
	l1.BrickColor = BrickColor.new("Black");
	l2.BrickColor = BrickColor.new("Black");
	l3.BrickColor = BrickColor.new("Black");
	l4.BrickColor = BrickColor.new("Black");
	l5.BrickColor = BrickColor.new("Black");
	l6.BrickColor = BrickColor.new("Black");
	wait(0.5);
	l1.BrickColor = BrickColor.new("Steel blue");
	l2.BrickColor = BrickColor.new("Steel blue");
	l3.BrickColor = BrickColor.new("Steel blue");
	l4.BrickColor = BrickColor.new("Steel blue");
	l5.BrickColor = BrickColor.new("Steel blue");
	l6.BrickColor = BrickColor.new("Steel blue");
	wait(0.5);
	l1.BrickColor = BrickColor.new("Black");
	l2.BrickColor = BrickColor.new("Black");
	l3.BrickColor = BrickColor.new("Black");
	l4.BrickColor = BrickColor.new("Black");
	l5.BrickColor = BrickColor.new("Black");
	l6.BrickColor = BrickColor.new("Black");
	wait(0.5);
	--2
	l1.BrickColor = BrickColor.new("Steel blue");
	l2.BrickColor = BrickColor.new("Black");
	l3.BrickColor = BrickColor.new("Black");
	l4.BrickColor = BrickColor.new("Black");
	l5.BrickColor = BrickColor.new("Black");
	l6.BrickColor = BrickColor.new("Black");
	wait(0.05);
	l1.BrickColor = BrickColor.new("Black");
	l2.BrickColor = BrickColor.new("Steel blue");
	l3.BrickColor = BrickColor.new("Black");
	l4.BrickColor = BrickColor.new("Black");
	l5.BrickColor = BrickColor.new("Black");
	l6.BrickColor = BrickColor.new("Black");
	wait(0.05);
	l1.BrickColor = BrickColor.new("Black");
	l2.BrickColor = BrickColor.new("Black");
	l3.BrickColor = BrickColor.new("Steel blue");
	l4.BrickColor = BrickColor.new("Black");
	l5.BrickColor = BrickColor.new("Black");
	l6.BrickColor = BrickColor.new("Black");
	wait(0.05);
	l1.BrickColor = BrickColor.new("Black");
	l2.BrickColor = BrickColor.new("Black");
	l3.BrickColor = BrickColor.new("Black");
	l4.BrickColor = BrickColor.new("Steel blue");
	l5.BrickColor = BrickColor.new("Black");
	l6.BrickColor = BrickColor.new("Black");
	wait(0.05);
	l1.BrickColor = BrickColor.new("Black");
	l2.BrickColor = BrickColor.new("Black");
	l3.BrickColor = BrickColor.new("Black");
	l4.BrickColor = BrickColor.new("Black");
	l5.BrickColor = BrickColor.new("Steel blue");
	l6.BrickColor = BrickColor.new("Black");
	wait(0.05);
	l1.BrickColor = BrickColor.new("Black");
	l2.BrickColor = BrickColor.new("Black");
	l3.BrickColor = BrickColor.new("Black");
	l4.BrickColor = BrickColor.new("Black");
	l5.BrickColor = BrickColor.new("Black");
	l6.BrickColor = BrickColor.new("Steel blue");
	wait(0.05);
	l1.BrickColor = BrickColor.new("Black");
	l2.BrickColor = BrickColor.new("Black");
	l3.BrickColor = BrickColor.new("Black");
	l4.BrickColor = BrickColor.new("Black");
	l5.BrickColor = BrickColor.new("Steel blue");
	l6.BrickColor = BrickColor.new("Black");
	wait(0.05);
	l1.BrickColor = BrickColor.new("Black");
	l2.BrickColor = BrickColor.new("Black");
	l3.BrickColor = BrickColor.new("Black");
	l4.BrickColor = BrickColor.new("Steel blue");
	l5.BrickColor = BrickColor.new("Black");
	l6.BrickColor = BrickColor.new("Black");
	wait(0.05);
	l1.BrickColor = BrickColor.new("Black");
	l2.BrickColor = BrickColor.new("Black");
	l3.BrickColor = BrickColor.new("Steel blue");
	l4.BrickColor = BrickColor.new("Black");
	l5.BrickColor = BrickColor.new("Black");
	l6.BrickColor = BrickColor.new("Black");
	wait(0.05);
	l1.BrickColor = BrickColor.new("Black");
	l2.BrickColor = BrickColor.new("Steel blue");
	l3.BrickColor = BrickColor.new("Black");
	l4.BrickColor = BrickColor.new("Black");
	l5.BrickColor = BrickColor.new("Black");
	l6.BrickColor = BrickColor.new("Black");
	wait(0.05);
	l1.BrickColor = BrickColor.new("Steel blue");
	l2.BrickColor = BrickColor.new("Black");
	l3.BrickColor = BrickColor.new("Black");
	l4.BrickColor = BrickColor.new("Black");
	l5.BrickColor = BrickColor.new("Black");
	l6.BrickColor = BrickColor.new("Black");
	--3
	wait(0.05);
	l1.BrickColor = BrickColor.new("Black");
	l2.BrickColor = BrickColor.new("Steel blue");
	l3.BrickColor = BrickColor.new("Black");
	l4.BrickColor = BrickColor.new("Black");
	l5.BrickColor = BrickColor.new("Black");
	l6.BrickColor = BrickColor.new("Black");
	wait(0.05);
	l1.BrickColor = BrickColor.new("Black");
	l2.BrickColor = BrickColor.new("Black");
	l3.BrickColor = BrickColor.new("Steel blue");
	l4.BrickColor = BrickColor.new("Black");
	l5.BrickColor = BrickColor.new("Black");
	l6.BrickColor = BrickColor.new("Black");
	wait(0.05);
	l1.BrickColor = BrickColor.new("Black");
	l2.BrickColor = BrickColor.new("Black");
	l3.BrickColor = BrickColor.new("Black");
	l4.BrickColor = BrickColor.new("Steel blue");
	l5.BrickColor = BrickColor.new("Black");
	l6.BrickColor = BrickColor.new("Black");
	wait(0.05);
	l1.BrickColor = BrickColor.new("Black");
	l2.BrickColor = BrickColor.new("Black");
	l3.BrickColor = BrickColor.new("Black");
	l4.BrickColor = BrickColor.new("Black");
	l5.BrickColor = BrickColor.new("Steel blue");
	l6.BrickColor = BrickColor.new("Black");
	wait(0.05);
	l1.BrickColor = BrickColor.new("Black");
	l2.BrickColor = BrickColor.new("Black");
	l3.BrickColor = BrickColor.new("Black");
	l4.BrickColor = BrickColor.new("Black");
	l5.BrickColor = BrickColor.new("Black");
	l6.BrickColor = BrickColor.new("Steel blue");
	wait(0.05);
	l1.BrickColor = BrickColor.new("Black");
	l2.BrickColor = BrickColor.new("Black");
	l3.BrickColor = BrickColor.new("Black");
	l4.BrickColor = BrickColor.new("Black");
	l5.BrickColor = BrickColor.new("Steel blue");
	l6.BrickColor = BrickColor.new("Black");
	wait(0.05);
	l1.BrickColor = BrickColor.new("Black");
	l2.BrickColor = BrickColor.new("Black");
	l3.BrickColor = BrickColor.new("Black");
	l4.BrickColor = BrickColor.new("Steel blue");
	l5.BrickColor = BrickColor.new("Black");
	l6.BrickColor = BrickColor.new("Black");
	wait(0.05);
	l1.BrickColor = BrickColor.new("Black");
	l2.BrickColor = BrickColor.new("Black");
	l3.BrickColor = BrickColor.new("Steel blue");
	l4.BrickColor = BrickColor.new("Black");
	l5.BrickColor = BrickColor.new("Black");
	l6.BrickColor = BrickColor.new("Black");
	wait(0.05);
	l1.BrickColor = BrickColor.new("Black");
	l2.BrickColor = BrickColor.new("Steel blue");
	l3.BrickColor = BrickColor.new("Black");
	l4.BrickColor = BrickColor.new("Black");
	l5.BrickColor = BrickColor.new("Black");
	l6.BrickColor = BrickColor.new("Black");
	wait(0.05);
	l1.BrickColor = BrickColor.new("Steel blue");
	l2.BrickColor = BrickColor.new("Black");
	l3.BrickColor = BrickColor.new("Black");
	l4.BrickColor = BrickColor.new("Black");
	l5.BrickColor = BrickColor.new("Black");
	l6.BrickColor = BrickColor.new("Black");
	wait(0.05);
	l1.BrickColor = BrickColor.new("Black");
	wait(0.5);

end;

Cheers

2 Likes

To be specific, I don’t mean putting it all on the same line, I mean actually simplifying it.

One thing you can do is use collectionservice, this will eliminate literally 90% of the code here and make it faster and simpler.
Hello again :blush:

Hello… again :neutral_face:

I have no clue on how to use CollectionService and have no plans to learn it. I’m sure it’s easier than I think, but I do not intend to go anywhere near it.

Couldyou try writing it, you seem fairly good with collectionservice.

If I am telling you to learn this, it probably means that its way easier than the copy and paste stuff your doing now

Definitely not for somebody who made his first script a month ago :slight_smile:

That’s like asking a toddler to create a rocket after learning its first words

I promise you it is incredibly simple, It should probably be one of the first things programmers learn. Look at the post I sent you.

Personally, I cant stand collection service. It just feels hacky and really weird, and I honestly would just rather iterate over a table.
Everyone has their style of course; but I don’t see a point in it if you can do it simply with such:

local PartTable = {l1, l2, l3...}

while true do
    for _, Part in PartTable do
       Part.BrickColor = BrickColor.new("Steel blue")
    end
  task.wait(0.5)
    for _, Part in PartTable do
       Part.BrickColor = BrickColor.new("Black")  
    end

... etc etc
end

Sorry, what exactly am I supposed to do with this script? It makes no sense

local brickColor1 = BrickColor.new("Steel blue")
local brickColor2 = BrickColor.new("Black")
local longerWaitTime = 0.5
local shorterWaitTime = 0.05
local numberOfLights = 6

local lightParts = {}
for lightIndex = 1, numberOfLights do
	lightParts[lightIndex] = script.Parent.Parent["L" .. lightIndex]
end

local function changeColorOfAllLights(brickColor)
	for _, part in lightParts do
		part.BrickColor = brickColor
	end
end

local function changeColorSuchThatOneHasColor1AndOthersHaveColor2(indexOfPartThatShouldHaveColor1)
	for i, part in lightParts do
		part.BrickColor = i == indexOfPartThatShouldHaveColor1 and brickColor1 or brickColor2
	end
end

while true do
	for firstPatternIteration = 1, 2 do
		changeColorOfAllLights(brickColor1)
		task.wait(longerWaitTime)
		changeColorOfAllLights(brickColor2)
		task.wait(longerWaitTime)
	end
	for secondPatternIteration = 1, 2 do
		for growingIndex = 1, numberOfLights do
			changeColorSuchThatOneHasColor1AndOthersHaveColor2(growingIndex)
			task.wait(shorterWaitTime)
		end
		for decreasingIndex = numberOfLights - 1, 1, -1 do
			changeColorSuchThatOneHasColor1AndOthersHaveColor2(decreasingIndex)
			task.wait(shorterWaitTime)
		end
	end
	changeColorOfAllLights(brickColor2)
	task.wait(longerWaitTime)
end
1 Like

What is LightTable to be assigned to? There is no variable for it.


image

int was just an accident. In C#, variable types need to be spesified, and int (integer) is a type. I accidentally mixed the syntax of the two languages. I’ve fixed that and made some other changes to the code. Does it still not work?

And for LightIndex? I assume in goes after int, as it will just put an error if I don’t add it

Sorry it only showed one edit. It works now.

I personally use collection service because instead of having multiple tables to control multiple things I can set tags to instances instead of manipulating individual items of tables.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.