How can i make this script shorter?

I’ll get straight to the point here! i am still a near beginner scripter, and i have quite a few long scripts in my game, They work fine, and no real bugs from what i can see, but they are ungodly long and i think they are causing lag?

these longer ones specifically are client side, and are being used for coloring, if there is any efficient way to make these shorter and easier to read i would really like to know.

Basically what i am trying to achieve is when a color button is clicked in a specific section (split into Sleeves, torso and extras) it will color the part.

here’s a piece of the longer ones, this is NOT the full script as there are about 21k lines in the actual one.

script.Parent.Jacket["Torso"].Red.Color1.MouseButton1Click:Connect(function() 
	local Color = Color3.new(1, 0.87451, 0.882353) 
	r:FireServer(Color)
	if game.Workspace["Clone morphs"]["false"]:FindFirstChild("jacket") then
		game.Workspace["Clone morphs"]["false"].jacket.Chest.Color = Color
		if game.Workspace["Clone morphs"]["false"].jacket:FindFirstChild("Waist") then
			game.Workspace["Clone morphs"]["false"].jacket.Waist.Color = Color
		end
		if game.Workspace["Clone morphs"]["false"].jacket:FindFirstChild("Neck") then
			game.Workspace["Clone morphs"]["false"].jacket.Neck.Color = Color
		end
		if game.Workspace["Clone morphs"]["false"].jacket:FindFirstChild("Torsoxtra1") then
			game.Workspace["Clone morphs"]["false"].jacket.Torsoxtra1.Color = Color
		end
		if game.Workspace["Clone morphs"]["false"].jacket:FindFirstChild("Torsoxtra2") then
			game.Workspace["Clone morphs"]["false"].jacket.Torsoxtra2.Color = Color
		end
		if game.Workspace["Clone morphs"]["false"].jacket:FindFirstChild("Torsoxtra3") then
			game.Workspace["Clone morphs"]["false"].jacket.Torsoxtra3.Color = Color
		end
		if game.Workspace["Clone morphs"]["false"].jacket:FindFirstChild("Torsoxtra4") then
			game.Workspace["Clone morphs"]["false"].jacket.Torsoxtra4.Color = Color
		end
		if game.Workspace["Clone morphs"]["false"].jacket:FindFirstChild("Torsoxtra5") then
			game.Workspace["Clone morphs"]["false"].jacket.Torsoxtra5.Color = Color
		end
		if game.Workspace["Clone morphs"]["false"].jacket:FindFirstChild("Torsoxtra6") then
			game.Workspace["Clone morphs"]["false"].jacket.Torsoxtra6.Color = Color
		end
	end
	
	script.Parent.Jacket["Torso"].CurrentColor.BackgroundColor3 = Color
end)

script.Parent.Jacket["Torso"].Red.Color2.MouseButton1Click:Connect(function() 
	local Color = Color3.new(1, 0.615686, 0.639216) 
	r:FireServer(Color)
	if game.Workspace["Clone morphs"]["false"]:FindFirstChild("jacket") then
		game.Workspace["Clone morphs"]["false"].jacket.Chest.Color = Color
		if game.Workspace["Clone morphs"]["false"].jacket:FindFirstChild("Waist") then
			game.Workspace["Clone morphs"]["false"].jacket.Waist.Color = Color
		end
		if game.Workspace["Clone morphs"]["false"].jacket:FindFirstChild("Neck") then
			game.Workspace["Clone morphs"]["false"].jacket.Neck.Color = Color
		end
		if game.Workspace["Clone morphs"]["false"].jacket:FindFirstChild("Torsoxtra1") then
			game.Workspace["Clone morphs"]["false"].jacket.Torsoxtra1.Color = Color
		end
		if game.Workspace["Clone morphs"]["false"].jacket:FindFirstChild("Torsoxtra2") then
			game.Workspace["Clone morphs"]["false"].jacket.Torsoxtra2.Color = Color
		end
		if game.Workspace["Clone morphs"]["false"].jacket:FindFirstChild("Torsoxtra3") then
			game.Workspace["Clone morphs"]["false"].jacket.Torsoxtra3.Color = Color
		end
		if game.Workspace["Clone morphs"]["false"].jacket:FindFirstChild("Torsoxtra4") then
			game.Workspace["Clone morphs"]["false"].jacket.Torsoxtra4.Color = Color
		end
		if game.Workspace["Clone morphs"]["false"].jacket:FindFirstChild("Torsoxtra5") then
			game.Workspace["Clone morphs"]["false"].jacket.Torsoxtra5.Color = Color
		end
		if game.Workspace["Clone morphs"]["false"].jacket:FindFirstChild("Torsoxtra6") then
			game.Workspace["Clone morphs"]["false"].jacket.Torsoxtra6.Color = Color
		end
	end
	script.Parent.Jacket["Torso"].CurrentColor.BackgroundColor3 = Color
end)

script.Parent.Jacket["Torso"].Red.Color3.MouseButton1Click:Connect(function() 
	local Color = Color3.new(0.956863, 0.341176, 0.376471) 
	r:FireServer(Color)
	if game.Workspace["Clone morphs"]["false"]:FindFirstChild("jacket") then
		game.Workspace["Clone morphs"]["false"].jacket.Chest.Color = Color
		if game.Workspace["Clone morphs"]["false"].jacket:FindFirstChild("Waist") then
			game.Workspace["Clone morphs"]["false"].jacket.Waist.Color = Color
		end
		if game.Workspace["Clone morphs"]["false"].jacket:FindFirstChild("Neck") then
			game.Workspace["Clone morphs"]["false"].jacket.Neck.Color = Color
		end
		if game.Workspace["Clone morphs"]["false"].jacket:FindFirstChild("Torsoxtra1") then
			game.Workspace["Clone morphs"]["false"].jacket.Torsoxtra1.Color = Color
		end
		if game.Workspace["Clone morphs"]["false"].jacket:FindFirstChild("Torsoxtra2") then
			game.Workspace["Clone morphs"]["false"].jacket.Torsoxtra2.Color = Color
		end
		if game.Workspace["Clone morphs"]["false"].jacket:FindFirstChild("Torsoxtra3") then
			game.Workspace["Clone morphs"]["false"].jacket.Torsoxtra3.Color = Color
		end
		if game.Workspace["Clone morphs"]["false"].jacket:FindFirstChild("Torsoxtra4") then
			game.Workspace["Clone morphs"]["false"].jacket.Torsoxtra4.Color = Color
		end
		if game.Workspace["Clone morphs"]["false"].jacket:FindFirstChild("Torsoxtra5") then
			game.Workspace["Clone morphs"]["false"].jacket.Torsoxtra5.Color = Color
		end
		if game.Workspace["Clone morphs"]["false"].jacket:FindFirstChild("Torsoxtra6") then
			game.Workspace["Clone morphs"]["false"].jacket.Torsoxtra6.Color = Color
		end
	end
	script.Parent.Jacket["Torso"].CurrentColor.BackgroundColor3 = Color
end)

if anyone can figure out how to optimize or shorten this script, it would be very much appreciated.

1 Like
local function changeColor(Color: Color3)
    r:FireServer(Color)
    local jacket = game.Workspace["Clone morphs"]["false"].jacket
    if jacket then
        jacket.Chest.Color = Color
        local extras = {"Waist", "Neck", "Torsoxtra1", "Torsoxtra2", "Torsoxtra3", "Torsoxtra4", "Torsoxtra5", "Torsoxtra6"}
        for _, extra in ipairs(extras) do
            if jacket:FindFirstChild(extra) then
                jacket[extra].Color = Color
            end
        end
    end
    script.Parent.Jacket["Torso"].CurrentColor.BackgroundColor3 = Color
end

script.Parent.Jacket["Torso"].Red.Color1.MouseButton1Click:Connect(function() 
    changeColor(Color3.new(1, 0.87451, 0.882353))
end)

script.Parent.Jacket["Torso"].Red.Color2.MouseButton1Click:Connect(function() 
    changeColor(Color3.new(1, 0.615686, 0.639216))
end)

script.Parent.Jacket["Torso"].Red.Color3.MouseButton1Click:Connect(function() 
    changeColor(Color3.new(0.956863, 0.341176, 0.376471))
end)

2 Likes

Loop through all descendants to check for a Color property.

Then change the color. Something like:


local BrickColor = Color3.fromRGB(255, 0, 0)


function HasProperty(object, propertyName)
	local success, _ = pcall(function() 
		object[propertyName] = object[propertyName]
	end)
	return success
end


for _, item in pairs(game.Workspace.Folder:GetDescendants()) do
	local success = HasProperty(item, "Color")
	if success then
		item.Color = BrickColor
		print("Success", item)
	end
end

Change Color.rbxl (53.5 KB)

1 Like

Mad respect for the dedication you put in to type all those if statements :joy:. Theres a few ways to shorten this script. However, they all use loops. @lavasance has a good solution if you only want to color specific items. However, if you want to color all of the objects, it would be as simple as looping through the children (via something like for _, item in workspace[“Clone morphs”][“false”]:GetChildren() do), checking the type of the instances (item:IsA(“MeshPart”) or something similar depending on what kind of instances are in it), and changing the items color (item.Color = color).

2 Likes

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