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
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
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: