My problem is that I want to check from the objects that appear on the area whether they touch each other, if so, then delete them. For some reason, this function deletes all objects and I do not understand why.
local function spawnObject(x,y,z,rY)
print("Spawning at position: (" ..x.. ", " ..y.. ", " ..z.. ")")--Display the spawn position of the object
local object=createObject()
if object then
object:SetPrimaryPartCFrame(CFrame.new(x,y,z+5)*rY)
--Check whether the coordinates of the object intersect with the coordinates of existing objects
local overlapParams=OverlapParams.new()
overlapParams.FilterType=Enum.RaycastFilterType.Include
overlapParams.FilterDescendantsInstances={folder}
local objectsInSpace=workspace:GetPartBoundsInRadius(object.PrimaryPart.Position,1,overlapParams)
--print(#objectsInSpace)
if #objectsInSpace>0 then
print("Object cannot be spawned due to overlap with existing objects.")
object:Destroy()
return
end
end
end