Combining Parts to reduce lag

So i have a script that makes a grid of parts, i want to combine parts of the same color to reduce part count
How it looks currently (Top) vs How i want it to look (Bottom) Example


how would i go about scripting this? i’m lost.
note : the parts i want to combine will always be in a grid like this, i do not care about other parts.

1 Like

I think what you’re looking for is trying to Union the parts together to form one Part. To do this you should use BasePart:UnionAsync()

Here is an example of this code:

local part = workspace.Part1
local Parts = {workspace.Part2, workspace.Part3}

local NewUnion = Parts:UnionAsync(Parts)

-- Destroys the old parts
part:Destroy()
for _, v in pairs(Parts) do
v:Destroy()
end

-- Inserts the new union
NewUnion.Parent = workspace

For more information about this, visit BasePart | Roblox Creator Documentation

i was under the impression unions cause more lag

Yes they do, I would not worry about lag. I’m not an expert but I think it’s more trouble than it’s worth to reduce part count.

Vouch, most large games get away with over 100k parts and from the screenshot you sent it doesn’t look like there are that many parts.

It’s known as greedy meshing check out these posts on it.

2 Likes

You can get these parts and try to check its sides for repeating colors, until there is no more of it. With that you can find the end corners of the repetition, you then repeat the process with the y coordinate. Finally, get the two resulting positions to create a block that will be the size of the x repetitions and y repetitions.

Thats how I saved world changes on a procedural world survival game I was making. Instead of saving 10000 positions where a player digged earth for a tunnel, the game saves only two corner positions to drastically reduce data usage. I later fill the space between these corners when the player reloads the world.

I hope you understood what I said as I am not good in explaining things.

local abs = workspace:WaitForChild("ABS")

local partColors = {}
local colors = {}

for _, part in ipairs(abs:GetChildren()) do
	table.insert(colors, tostring(part.BrickColor).." : "..part.Name)
end

table.sort(colors, function(a, b)
	return tostring(a) < tostring(b)
end)

for i, v in ipairs(colors) do
	local split = string.split(v, " : ")
	local item = {[split[2]] = split[1]}
	table.insert(partColors, item)
end

for i, v in ipairs(partColors) do
	for i, tab in pairs(v) do
		print(i, tab)
	end
end

Parts need to be named differently.
Output with a random model of parts: