Break part into 8 pieces

  1. What do you want to achieve? Keep it simple and clear!
    I want to break parts into various 2x2x2 parts.
    Example: 4x4x4 into 8 parts.

  2. What is the issue? Include screenshots / videos if possible!
    I can’t find a way to do it.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried searching in the dev forum and also using:

for i=2, target.Size.X do
-- Code
end

This forum post might help…I was looking up something similar.

1 Like
local function seperate(part:BasePart)
	local halfX = part.Size.X/2
	local halfY = part.Size.Y/2
	local halfZ = part.Size.Z/2
	local smallSize = Vector3.new(halfX,halfY,halfZ)
	local cornerPosition = part.Position - smallSize
	
	for i = 0,7 do
		local smallPart = part:Clone()
		smallPart.Parent = part.Parent
		smallPart.Size = smallSize
		
		local xUnits = 0
		if i >= 4 then
			i-= 4
			xUnits = 1
		end
		
		local yUnits = 0
		if i >= 2 then
			i -= 2
			yUnits = 1
		end
		
		local zUnits = 0
		if i >= 1 then
			zUnits = 1
		end
		
		local basePosition = cornerPosition + Vector3.new(xUnits * halfX,yUnits * halfY,zUnits * halfZ)
		smallPart.Position = basePosition + Vector3.new(halfX/2,halfY/2,halfZ/2)
	end
	
	part:Destroy()
end

What is the context of using this? I’m wondering because wouldn’t it be viable to just make it 8 pieces all together to begin with?

local function subdivide(part, levels)
    local pointsPerAxis = math.pow(2, levels)
    local pSize = part.Size / pointsPerAxis
    local corner = part.Position - part.Size/2 + pSize/2
    local positions = {}
    for i=0, pointsPerAxis do
        for j=0, pointsPerAxis do
            for k=0, pointsPerAxis do
                table.insert(positions, corner + pSize * Vector3.new(i, j, k)
            end
        end
    end
    
    for _, position in ipairs(positions) do --This could be done above, but I wanted to split it out simply for better distinction between parts.  I recommend using a parts cache actually if you can because the :Clone() operation is probably slow.
        local p = part:Clone()
        p.Size = pSize
        p.Position = position --This does techincally assume axis alignment.  The position list can be easily made to be in object space though so you can trivially fix that
    end
    part:Destroy() --optional
end

Note this is untested

Nice one, but Shr0_0m already did it. Thank you anyways!

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