Strange cloning logic

I’ve been having a crack at this for hours! I don’t know if I’ve gone insane or something but can someone explain the logic behind why the CSG is repeatedly cloning itself (e.g: each fragment duplicates the exact same geometry x times)? Thanks:

local function FracturePart(part: BasePart, depth: number?)
	depth = depth or 0

	if not part:IsDescendantOf(workspace) then return end
	if FRAGMENT_RECURSES < depth then return end

	local angularCFrame = generateRandomAngularCFrame()
	local cutPlane = part.CFrame * angularCFrame

	local firstCut = Instance.new("Part")
	firstCut.Size = part.Size * CUT_SIZE_MULTIPLIER
	firstCut.CFrame = cutPlane * CFrame.new(0, -part.Size.Y * 2, 0)

	local secondCut = firstCut:Clone()
	secondCut.CFrame = cutPlane * CFrame.new(0, part.Size.Y * 2, 0)

	local firstSubtraction = GeometryService:SubtractAsync(part, { firstCut }, SUBTRACTION_OPTIONS)
	local secondSubtraction = GeometryService:SubtractAsync(part, { secondCut }, SUBTRACTION_OPTIONS)

	local function handleSubtractions(subtractionList: { PartOperation })
		for index, subtraction in subtractionList do
			local cframe = part.CFrame * part.CFrame:ToObjectSpace(subtraction.CFrame)
			subtraction.Parent = part.Parent
			subtraction.CFrame = cframe
			subtraction.Anchored = false -- part.Anchored
			subtraction.AssemblyLinearVelocity = part.AssemblyLinearVelocity
			subtraction.AssemblyAngularVelocity = part.AssemblyAngularVelocity

			task.defer(FracturePart, subtraction, depth + 1) -- i don't actually know the logic which makes it clone
			table.insert(DEBRIS, subtraction)
		end
	end

	task.spawn(handleSubtractions, firstSubtraction)
	task.spawn(handleSubtractions, secondSubtraction)
	
	if part then part:Destroy() end
end
1 Like