Greedy meshing destroying parts

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? make my greedy meshing + rounding algorithm not destroy parts

  2. What is the issue? Include screenshots / videos if possible!

External Media
  1. What solutions have you tried so far? alot of them

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

This is my greedy mesher:

local module = {}

function module.Mesh(Model, Jump, MeshIters)
	if not Jump then
		Jump = 0.05
	end
	if not MeshIters then
		MeshIters = 5
	end

	local function CastX(v)
		local raycastResultPX = workspace:Raycast(v.Position, Vector3.new(v.Size.X+Jump, 0, 0))
		if raycastResultPX then
			if raycastResultPX.Instance then

				if raycastResultPX.Instance.Size.Y == v.Size.Y and raycastResultPX.Instance.Size.Z == v.Size.Z and raycastResultPX.Instance.Parent == Model and raycastResultPX ~= v then
					local ogSize = v.Size.X
					local Dist = (v.Position.X-v.Size.X/2)+(raycastResultPX.Instance.Position.X+raycastResultPX.Instance.Size.X/2)
					v.Size = Vector3.new(math.abs((v.Position.X-v.Size.X/2)-(raycastResultPX.Instance.Position.X+raycastResultPX.Instance.Size.X/2)), v.Size.Y, v.Size.Z)
					--	v.Position = v.Position + Vector3.new(0, 0, (ogSize/2) + ((raycastResultPZ.Instance.Size.Z-ogSize)/2))
					v.Position = Vector3.new(Dist/2, v.Position.Y, v.Position.Z)
					raycastResultPX.Instance:Destroy()
					return v
				end

			end
		else
			return
		end
	end
	local function CastY(v)
		local raycastResultPY = workspace:Raycast(v.Position, Vector3.new(0, v.Size.Y+Jump, 0))
		if raycastResultPY then
			if raycastResultPY.Instance then

				if raycastResultPY.Instance.Size.X == v.Size.X and raycastResultPY.Instance.Size.Z == v.Size.Z and raycastResultPY.Instance.Parent == Model and raycastResultPY ~= v   then
					local ogSize = v.Size.Y
					v.Size = v.Size + Vector3.new(0, raycastResultPY.Instance.Size.Y, 0)
					v.Position = v.Position + Vector3.new(0, (ogSize/2) + ((raycastResultPY.Instance.Size.Y-ogSize)/2), 0)
					raycastResultPY.Instance:Destroy()
					return v
				end

			end
		else
			return
		end
	end
	local function CastZ(v)
		local raycastResultPZ = workspace:Raycast(v.Position, Vector3.new(0, 0, v.Size.Z+Jump))
		if raycastResultPZ then
			if raycastResultPZ.Instance then

				if raycastResultPZ.Instance.Size.X == v.Size.X and raycastResultPZ.Instance.Size.Y == v.Size.Y and raycastResultPZ.Instance.Parent == Model and raycastResultPZ ~= v   then
					local ogSize = v.Size.Z
						v.Size = v.Size + Vector3.new(0, 0, raycastResultPZ.Instance.Size.Z)
					--local Dist = (v.Position.Z-v.Size.Z/2)+(raycastResultPZ.Instance.Position.Z+raycastResultPZ.Instance.Size.Z/2)
				--	v.Size = Vector3.new(v.Size.X, v.Size.Y, math.abs((v.Position.Z-v.Size.Z/2)-(raycastResultPZ.Instance.Position.Z+raycastResultPZ.Instance.Size.Z/2)))
						v.Position = v.Position + Vector3.new(0, 0, (ogSize/2) + ((raycastResultPZ.Instance.Size.Z-ogSize)/2))
					--v.Position = Vector3.new(v.Position.X, v.Position.Y, Dist/2)
					raycastResultPZ.Instance:Destroy()
					return v
				end

			end
		else
			return
		end
	end
	local OX, OY, OZ = Model.WorldPivot:ToOrientation()
	Model:PivotTo(CFrame.new(Model.WorldPivot.Position)*CFrame.Angles(0, 0, 0))
	for i, v in pairs(Model:GetChildren()) do
		if v:IsA("Part") then

			for i=0, MeshIters, 1 do
				resultX = CastX(v)
				if resultX == nil then
					break
				end

			end
			if i==0 then
				for i3=0, MeshIters, 1 do
					resultZ = CastZ(v)
					if resultZ == nil then
						break
					end
				end
				if i==0 and i3==0 then 

					for i2=0, MeshIters, 1 do
						resultY = CastY(v)
						if resultY == nil then
							break
						end
					end
				end
			end
		end
	end
	Model:PivotTo(CFrame.new(Model.WorldPivot.Position)*CFrame.Angles(OX, OY, OZ))
end


return module

and this is the code interacting with it:

	local IsDone
	local ObjList
	for i, v in pairs(script.Parent.Parent.deczone:GetTouchingParts()) do
		if v.Parent == game.Workspace.Model then
			IsDone, ObjList = Module.SingleSubtract(v, script.Parent.Parent.subzone)
			wait(IsDone == true)
		end
	end
	for i, v in pairs(game.Workspace.Model:GetChildren()) do
	--	print(v)
		if v:IsA("Part") then
			if v.Size.X < 1/Resolution or v.Size.Y < 1/Resolution or v.Size.Z < 1/Resolution then
				v:Destroy()
			end
			
		
			--	Resolution = 5
			--v.Position = Vector3.new(math.round(v.Position.x*Resolution)/Resolution, math.round(v.Position.y*Resolution)/Resolution, math.round(v.Position.z*Resolution)/Resolution)

			v.Size = Vector3.new(math.round(v.Size.x*Resolution)/Resolution, math.round(v.Size.y*Resolution)/Resolution, math.round(v.Size.z*Resolution)/Resolution)
			v.Position = Vector3.new(math.round(v.Position.x*(Resolution*50))/(Resolution*50), math.round(v.Position.y*(Resolution*50))/(Resolution*50), math.round(v.Position.z*(Resolution*50))/(Resolution*50))

			
			if Count > 60 then
				RectMesher.Mesh(game.Workspace.Model)
				wait(0.1)
				Count = 0
			end
			v.Size = Vector3.new(math.round(v.Size.x*Resolution)/Resolution, math.round(v.Size.y*Resolution)/Resolution, math.round(v.Size.z*Resolution)/Resolution)
		--	v.Position = Vector3.new(math.round(v.Position.x*Resolution)/Resolution, math.round(v.Position.y*Resolution)/Resolution, math.round(v.Position.z*Resolution)/Resolution)
--game.Workspace.Model.PrimaryPart = v
			--v.Orientation = Vector3.new(0, 0, 0)
		end
	end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

bumping cause i need help rlly bad