Problem with OverlapParams

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
1 Like

It is probably overlapping with other parts in the object
So you should loop through objectsInSpace to find if there is a part not in the object

Also SetPrimaryPartCFrame is deprecated you should use PivotTo

2 Likes

Thank you. Your method worked.

1 Like

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