How do I fill a part with more parts using scripting?

Hi! I am wanting to make a destruction game where you destroy parts with explosions, but I want a bunch of smaller parts that make up the map. To do this I want to fill a part,
this for example, with other smaller parts.

For example

- See how this is made up of multiple parts? How could I fill a space with more smaller parts using a script?

Thank you - Lux

This is a fairly hard geometry problem and you can significantly limit how hard it is by limiting what cases you want to model. Do you just want to be able to run this in studio or have it work in real time when the part takes damage?

1 Like

Just be able to run this in studio, or when it takes damage. I don’t think either would be far from each other.

When a part takes damage, how do you want it to be divided? The easiest case would be to divide a block into two, four, or eight parts along its center. The harder way is to divide it unevenly around the point where damage is taken / point of impact. The hardest way would be to divide it enough times to allow a “crater” to be cut out of it. These build off of each other, so I would advise you do the easy one first and then decide if its good enough. Do you feel comfortable doing the first one, in terms of your math and scripting ability?

What do you mean divide it ino 8 parts along its center? How would I do that

Like this?

1 Like

Code to do this:

local bipolarVectorSet = {
	Vector3.new(1,1,1),
	Vector3.new(1,1,-1),
	Vector3.new(1,-1,1),
	Vector3.new(1,-1,-1),
	Vector3.new(-1,1,1),
	Vector3.new(-1,1,-1),
	Vector3.new(-1,-1,1),
	Vector3.new(-1,-1,-1),
}

local function DivideBlock(block : Instance)
	local halfSize = block.Size / 2.0
	
	for _, offsetVector in pairs(bipolarVectorSet) do
		
		local clone = block:Clone()
		clone.Parent = workspace
		clone.Size = halfSize
		clone.CFrame += block.CFrame:VectorToWorldSpace((halfSize / 2.0) * offsetVector)
	end
	
	block:Destroy()
end

Just call DivideBlock() and pass the block you want to cut.

4 Likes

If the part to “fill/divide” is a sphere or cylinder? do you think there is a way to do it? maybe getting region from part?

There isnt a good way to cut a sphere or cylinder exactly, except for the special case of cutting a cylinder into discs. My workaround would be to break those into blocks that closely approximate the shape, similar to how you’d make a sphere or cylinder in minecraft.

2 Likes