Script Efficiency

So I recently wrote this script for a traffic light, r1 is the red light for 1 of the signals, and r2 is the second direction signal etc. Is there anyway I can make this script more efficient and less big?

r1 = script.Parent.R1
y1 = script.Parent.Y1
g1 = script.Parent.G1
r2 = script.Parent.R2
y2 = script.Parent.Y2
g2 = script.Parent.G2
r3 = script.Parent.R3
y3 = script.Parent.Y3
g3 = script.Parent.G3
r4 = script.Parent.R4
y4 = script.Parent.Y4
g4 = script.Parent.G4
s1 = script.Parent.S1

while true do
	r1.Material = "Neon"
	y1.Material = "SmoothPlastic"
	g1.Material = "SmoothPlastic"
	r2.Material = "SmoothPlastic"
	y2.Material = "SmoothPlastic"
	g2.Material = "Neon"
	r3.Material = "SmoothPlastic"
	y3.Material = "SmoothPlastic"
	g3.Material = "Neon"
	r4.Material = "Neon"
	y4.Material = "SmoothPlastic"
	g4.Material = "SmoothPlastic"
	wait(2)
	r1.Material = "SmoothPlastic"
	y1.Material = "Neon"
	g1.Material = "SmoothPlastic"
	r2.Material = "SmoothPlastic"
	y2.Material = "Neon"
	g2.Material = "SmoothPlastic"
	r3.Material = "SmoothPlastic"
	y3.Material = "Neon"
	g3.Material = "SmoothPlastic"
	r4.Material = "SmoothPlastic"
	y4.Material = "Neon"
	g4.Material = "SmoothPlastic"
	wait(2)
	r1.Material = "SmoothPlastic"
	y1.Material = "SmoothPlastic"
	g1.Material = "Neon"
	r2.Material = "Neon"
	y2.Material = "SmoothPlastic"
	g2.Material = "SmoothPlastic"
	r3.Material = "Neon"
	y3.Material = "SmoothPlastic"
	g3.Material = "SmoothPlastic"
	r4.Material = "SmoothPlastic"
	y4.Material = "SmoothPlastic"
	g4.Material = "Neon"
	wait(2)
end
2 Likes

I’ve made your code more object-oriented, using “signal” objects. You can rewrite the while loop using the SetSignalState() function.

r1 = script.Parent.R1
y1 = script.Parent.Y1
g1 = script.Parent.G1
r2 = script.Parent.R2
y2 = script.Parent.Y2
g2 = script.Parent.G2
r3 = script.Parent.R3
y3 = script.Parent.Y3
g3 = script.Parent.G3
r4 = script.Parent.R4
y4 = script.Parent.Y4
g4 = script.Parent.G4
s1 = script.Parent.S1


local Signals = {
	{Name = "Signal1", RedLight = r1, GreenLight = g1, YellowLight = y1},
	{Name = "Signal2", RedLight = r2, GreenLight = g2, YellowLight = y2}
}





function SetSignalState(signal, state : number) -- State = 1: Green  2: Yellow  3: Red
	if state == 1 then
		signal.GreenLight.Material = Enum.Material.Neon
		signal.RedLight.Material = Enum.Material.SmoothPlastic
		signal.YellowLight.Material = Enum.Material.SmoothPlastic
	elseif state == 2 then
		signal.GreenLight.Material = Enum.Material.SmoothPlastic
		signal.RedLight.Material = Enum.Material.SmoothPlastic
		signal.YellowLight.Material = Enum.Material.Neon
	elseif state == 3 then
		signal.GreenLight.Material = Enum.Material.SmoothPlastic
		signal.RedLight.Material = Enum.Material.Neon
		signal.YellowLight.Material = Enum.Material.SmoothPlastic
	end
end


--EXAMPLE--
SetSignalState(Signals[1], 1) -- Signal 1. State 1 for green.



Hello, He is my attempt at Optimizing the script as much as possible.

local Part = {
    {script.Parent.R1, script.Parent.Y1, script.Parent.G1},
    {script.Parent.R2, script.Parent.Y2, script.Parent.G2},
    {script.Parent.R3, script.Parent.Y3, script.Parent.G3},
    {script.Parent.R4, script.Parent.Y4, script.Parent.G4}
}
local function SetMaterial(material, partGroup)
    for _, part in ipairs(partGroup) do
        part.Material = material
    end
end

local Sequences = {
    {"Neon", "SmoothPlastic", "SmoothPlastic"},
    {"SmoothPlastic", "Neon", "SmoothPlastic"},
    {"SmoothPlastic", "SmoothPlastic", "Neon"}
}

local Index = 1
while true do
    local Sequence = Sequences[Index]
    for i, material in ipairs(Sequence) do
        SetMaterial(material, Part[i])
    end
    task.wait(2)
    Index = (Index % #Sequences) + 1
end

This looks like a better alternative, can you break down the functions of the script?

I like the creativity there, this seems like a good alternative as well

I start by defining the ‘Part’ table, which contains the references to the traffic light parts

local Part = {
    {script.Parent.R1, script.Parent.Y1, script.Parent.G1},
    {script.Parent.R2, script.Parent.Y2, script.Parent.G2},
    {script.Parent.R3, script.Parent.Y3, script.Parent.G3},
    {script.Parent.R4, script.Parent.Y4, script.Parent.G4}
}

Then, I make a function called ‘SetMaterial’ which takes two Arguments ‘material’ and ‘partGroup’.

The function then loops through the ‘partGroup’ and for every part, it sets that part’s material to the corresponding material.

local function SetMaterial(material, partGroup)
    for _, part in ipairs(partGroup) do
        part.Material = material
    end
end

Then, I make another table named ‘Sequences’ which holds the 3 tables that are the sequence of materials I want the Parts to be.

local Sequences = {
    {"Neon", "SmoothPlastic", "SmoothPlastic"},
    {"SmoothPlastic", "Neon", "SmoothPlastic"},
    {"SmoothPlastic", "SmoothPlastic", "Neon"}
}

Then for the loop, I make a variable named ‘Index’. This variable is used to keep track of which material is to be used.

local Index = 1

Then, made another Variable inside the Loop named ‘Sequence’ to get the correct sequence table.

local Sequence = Sequences[Index]

Then, loop through the Sequence table and fire the SetMaterial function on the Material and Part.

for i, material in ipairs(Sequence) do
   SetMaterial(material, Part[I])
end

Finally, wait 2 seconds, and update ‘Index’ to the next available sequence, wrapping around if necessary.

Index = (Index % #Sequences) + 1
3 Likes

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