How to optimize my code

Hi everyone, I am trying to optimize my code but my methods don’t work…
I have a lot of parts referenced in the game that need to change to the SAME color. I tried making a list of all of them but it didn’t work. How should I do this? My goal would be to have just one variable for all the parts that change to that one color.
Here is the code (its long…):

local Players = game:GetService("Players")
local plr = Players.LocalPlayer

local TweenService = game:GetService("TweenService")
local Info = TweenInfo.new(5, Enum.EasingStyle.Linear)

local Ceiling = game.Workspace.TheUltimateCeiling
local Borders = game.Workspace.Borders

local Button = game.Workspace.Button
local ButtonClick = Button.ClickDetector

local NeonPart1 = Borders.NeonParts.NeonPart1
local NeonPart2 = Borders.NeonParts.NeonPart2
local NeonPart3 = Borders.NeonParts.NeonPart3
local NeonPart4 = Borders.NeonParts.NeonPart4
local NeonPart5 = Borders.NeonParts.NeonPart5
local NeonPart6 = Borders.NeonParts.NeonPart6
local NeonPart7 = Borders.NeonParts.NeonPart7
local NeonPart8 = Borders.NeonParts.NeonPart8
local NeonPart9 = Borders.NeonParts.NeonPart9
local NeonPart10 = Borders.NeonParts.NeonPart10
local NeonPart11 = Borders.NeonParts.NeonPart11
local NeonPart12 = Borders.NeonParts.NeonPart12
local NeonPart13 = Borders.NeonParts.NeonPart13
local NeonPart14 = Borders.NeonParts.NeonPart14
local NeonPart15 = Borders.NeonParts.NeonPart15
local NeonPart16 = Borders.NeonParts.NeonPart16

local Part1 = Borders.Borders.Part1
local Part2 = Borders.Borders.Part2
local Part3 = Borders.Borders.Part3
local Part4 = Borders.Borders.Part4
local Part5 = Borders.Borders.Part5
local Part6 = Borders.Borders.Part6
local Part7 = Borders.Borders.Part7
local Part8 = Borders.Borders.Part8
local Part9 = Borders.Borders.Part9
local Part10 = Borders.Borders.Part10
local SecondCeiling = Borders.Borders.TheCeilingButItsLessCool

-- >> CeilingFalling: Function

function CeilingFalling()
	
	NeonPart1.BrickColor = BrickColor.Red()
	NeonPart2.BrickColor = BrickColor.Red()
	NeonPart3.BrickColor = BrickColor.Red()
	NeonPart4.BrickColor = BrickColor.Red()
	NeonPart5.BrickColor = BrickColor.Red()
	NeonPart6.BrickColor = BrickColor.Red()
	NeonPart7.BrickColor = BrickColor.Red()
	NeonPart8.BrickColor = BrickColor.Red()
	NeonPart9.BrickColor = BrickColor.Red()
	NeonPart10.BrickColor = BrickColor.Red()
	NeonPart11.BrickColor = BrickColor.Red()
	NeonPart12.BrickColor = BrickColor.Red()
	NeonPart13.BrickColor = BrickColor.Red()
	NeonPart14.BrickColor = BrickColor.Red()
	NeonPart15.BrickColor = BrickColor.Red()
	NeonPart16.BrickColor = BrickColor.Red()
	
	Part1.BrickColor = BrickColor.new("Salmon")
	Part2.BrickColor = BrickColor.new("Salmon")
	Part3.BrickColor = BrickColor.new("Salmon")
	Part4.BrickColor = BrickColor.new("Salmon")
	Part5.BrickColor = BrickColor.new("Salmon")
	Part6.BrickColor = BrickColor.new("Salmon")
	Part7.BrickColor = BrickColor.new("Salmon")
	Part8.BrickColor = BrickColor.new("Salmon")
	Part9.BrickColor = BrickColor.new("Salmon")
	Part10.BrickColor = BrickColor.new("Salmon")
	SecondCeiling.BrickColor = BrickColor.new("Salmon")
	
	Ceiling.BrickColor = BrickColor.new("Daisy orange")
	
	wait(3)
	
	Ceiling.BrickColor = BrickColor.Red()
	
	local Anim = {Position = Vector3.new(-41.5, 59.5, 19.5)}
	
	local Tween = TweenService:Create(Ceiling, Info, Anim)
	Tween:Play()
	
	wait(7)
	local Info = TweenInfo.new(2.5, Enum.EasingStyle.Linear)
	
	local Anim = {Position = Vector3.new(-41.5, 709.5, 19.5)}
	
	local Tween = TweenService:Create(Ceiling, Info, Anim)
	Tween:Play()
	
	wait(3.5)
	
	NeonPart1.BrickColor = BrickColor.new("Lime green")
	NeonPart2.BrickColor = BrickColor.new("Lime green")
	NeonPart3.BrickColor = BrickColor.new("Lime green")
	NeonPart4.BrickColor = BrickColor.new("Lime green")
	NeonPart5.BrickColor = BrickColor.new("Lime green")
	NeonPart6.BrickColor = BrickColor.new("Lime green")
	NeonPart7.BrickColor = BrickColor.new("Lime green")
	NeonPart8.BrickColor = BrickColor.new("Lime green")
	NeonPart9.BrickColor = BrickColor.new("Lime green")
	NeonPart10.BrickColor = BrickColor.new("Lime green")
	NeonPart11.BrickColor = BrickColor.new("Lime green")
	NeonPart12.BrickColor = BrickColor.new("Lime green")
	NeonPart13.BrickColor = BrickColor.new("Lime green")
	NeonPart14.BrickColor = BrickColor.new("Lime green")
	NeonPart15.BrickColor = BrickColor.new("Lime green")
	NeonPart16.BrickColor = BrickColor.new("Lime green")
	
	Part1.BrickColor = BrickColor.new("Mint")
	Part2.BrickColor = BrickColor.new("Mint")
	Part3.BrickColor = BrickColor.new("Mint")
	Part4.BrickColor = BrickColor.new("Mint")
	Part5.BrickColor = BrickColor.new("Mint")
	Part6.BrickColor = BrickColor.new("Mint")
	Part7.BrickColor = BrickColor.new("Mint")
	Part8.BrickColor = BrickColor.new("Mint")
	Part9.BrickColor = BrickColor.new("Mint")
	Part10.BrickColor = BrickColor.new("Mint")
	SecondCeiling.BrickColor = BrickColor.new("Mint")
	
	Ceiling.BrickColor = BrickColor.new("Pastel green")
	
end

please use https://devforum.roblox.com/c/help-and-feedback/code-review/

1 Like
local parts = game.Workspace.Parts

for _,parts in pairs(parts:GetChildren()) do
--funtions in here.
end

You should use loops to make changes to multiple parts at once. I have adjusted your code using the changes I suggested. Try using it and let me know what happens.

local Players = game:GetService("Players")
local plr = Players.LocalPlayer

local TweenService = game:GetService("TweenService")
local Info = TweenInfo.new(5, Enum.EasingStyle.Linear)

local Ceiling = game.Workspace.TheUltimateCeiling
local Borders = game.Workspace.Borders

local Button = game.Workspace.Button
local ButtonClick = Button.ClickDetector

local NeonParts = Borders.NeonParts -- The neon parts should be in this group.

local Parts = Borders.Borders -- The parts should be in this group.

local SecondCeiling = Borders.Borders.TheCeilingButItsLessCool

-- >> CeilingFalling: Function

function CeilingFalling()
	
	-- Make a loop so that each part inside NeonParts would be changed to the same color.
	for _,parts in pairs(NeonParts:GetChildren()) do
		parts.BrickColor = BrickColor.Red()
	end
	
	-- Make a loop so that each part inside Parts would be changed to the same color.
	for _,parts in pairs(Parts:GetChildren()) do
		parts.BrickColor = BrickColor.new("Salmon")
	end

	SecondCeiling.BrickColor = BrickColor.new("Salmon")
	
	Ceiling.BrickColor = BrickColor.new("Daisy orange")
	
	wait(3)
	
	Ceiling.BrickColor = BrickColor.Red()
	
	local Anim = {Position = Vector3.new(-41.5, 59.5, 19.5)}
	
	local Tween = TweenService:Create(Ceiling, Info, Anim)
	Tween:Play()
	
	wait(7)
	local Info = TweenInfo.new(2.5, Enum.EasingStyle.Linear)
	
	local Anim = {Position = Vector3.new(-41.5, 709.5, 19.5)}
	
	local Tween = TweenService:Create(Ceiling, Info, Anim)
	Tween:Play()
	
	wait(3.5)

	for _,parts in pairs(NeonParts:GetChildren()) do
		parts.BrickColor = BrickColor.new("Lime green")
	end
	
	for _,parts in pairs(Parts:GetChildren()) do
		parts.BrickColor = BrickColor.new("Mint")
	end

	SecondCeiling.BrickColor = BrickColor.new("Mint")
	
	Ceiling.BrickColor = BrickColor.new("Pastel green")
	
end
2 Likes

Thank you so much. this worked!

1 Like

an alternative solution could be using CollectionService

1 Like