I had the same problem initially when i came across this, You have edit the module so that the parts are initially generated once and the touch event isn’t as “OverLoaded” , instead of multiple times which causes the game to crash.
CodeExample
Here a modified version of this code: (its a bit messy but you should get the idea, hopefully)
local CSGService = {}
--inital
local RIGHT = Vector3.new(1, 0, 0)
local UP = Vector3.new(0, 1, 0)
local BACK = Vector3.new(0, 0, 1)
local CurrentUnUsedFolder
local oldcanvascf
local OldcanvasSize
local function vec3Func(f, v)
return Vector3.new(f(v.x), f(v.y), f(v.z))
end
local function slice(part, wPoint, axis, part2)
local pSize, pCF = part.Size, part.CFrame
local pRot = pCF - pCF.p
local length = axis:Dot(pSize)
local mSize = pSize - axis*length
local max, min = length/2, -length/2
local split = axis:Dot(pCF:PointToObjectSpace(wPoint))
if (split >= min and split <= max) then
local l1, l2 = max - split, split - min
local p1, p2 = pCF*(axis*(l1/2 + split)), pCF*(axis*(-l2/2 + split))
local part1 = part
part1.CFrame = CFrame.new(p1) * pRot
part1.Size = mSize + axis*l1
if part2 then
part2.CFrame = CFrame.new(p2) * pRot
part2.Size = mSize + axis*l2
part2.Parent = CurrentUnUsedFolder
return part1, part2
end
end
return part
end
function CSGService.Cut(part, canvas, Folder, Folder2)
if part then
part.Orientation = Vector3.new(90, 180 + canvas[1].Orientation.Y , 0) --- you can mess around with this, i just used this just for my configuration
CurrentUnUsedFolder = Folder2
local cf, size2 = part.CFrame, part.Size/2
local canvasCF = canvas[1].CFrame
local axis = Vector3.FromAxis(Enum.Axis.Z)
local p1, p2 = cf*(axis*size2), cf*(-axis*size2)
local sliceAxis = canvasCF:VectorToObjectSpace(cf:VectorToWorldSpace(axis))
sliceAxis = vec3Func(function(c) return math.min(1, math.abs(c)) end, sliceAxis)
for i = 1, #canvas do
local Children =Folder:GetChildren()
local a, b = slice(canvas[i], p1, sliceAxis, Children[i] )
canvas[#canvas+1] = a
canvas[#canvas+1] = b
end
for i = 1, #canvas do
local Children = Folder:GetChildren()
local a, b = slice(canvas[i], p2, sliceAxis, Children[i] )
canvas[#canvas+1] = a
canvas[#canvas+1] = b
end
local axis = Vector3.FromAxis(Enum.Axis.X)
local p1, p2 = cf*(axis*size2), cf*(-axis*size2)
local sliceAxis = canvasCF:VectorToObjectSpace(cf:VectorToWorldSpace(axis))
sliceAxis = vec3Func(function(c) return math.min(1, math.abs(c)) end, sliceAxis)
for i = 1, #canvas do
local Children = Folder:GetChildren()
local a, b = slice(canvas[i], p1, sliceAxis, Children[i])
canvas[#canvas+1] = a
canvas[#canvas+1] = b
end
for i = 1, #canvas do
local Children = Folder:GetChildren()
local a, b = slice(canvas[i], p2, sliceAxis, Children[i])
canvas[#canvas+1] = a
canvas[#canvas+1] = b
end
local lookup = {}
for i = 1, #canvas do
lookup[canvas[i]] = true
end
for i,v in pairs(Folder2:GetChildren()) do
if v then
v.Transparency = 0
v.CanCollide =true
v.Parent = Folder
end
end
local t = part.Touched:Connect(function() end)
local touching = part:GetTouchingParts()
t:Disconnect()
for i = 1, #touching do
if (lookup[touching[i]]) then
if touching[i].Name == "Part" then
touching[i].Transparency = 1
touching[i].CanCollide = false
end
end
end
end
end
return CSGService
---------Script
local CSgservice = require(game.ReplicatedStorage.CSGService)
----insert parts into the MainFolder
local NumberOfParts = 22--- i found 22 parts to be enough to cover a whole wall/part, You might want to add a way to calculte this instead
for i = 1, NumberOfParts do
local Part = Instance.new("Part")
Part.Transparency = .5
Part.Parent = MainFolder
Part.Anchored = true
Part.CastShadow = false
end
CSgservice.Cut(Part, Canvas, MainFolder, Temporary Folder)