Detect if 2 models are colliding/clipping

I have a script that clones models into a plane and positions them in random locations(via a primary part), the problem is that sometimes the models collide/clip trough each other due to the randomness.
image

Is there a way to check if the parts in the latest cloned model are colliding with the parts of a previous one? so that if they are I can delete the model and try again with a new random location

1 Like

set CanTouch to true, then using part:GetTouchingParts() check if it is colliding

That’s not always reliable, it’s better to make a single huge part or region3 as a hitbox.

local model1 = workspace:WaitForChild("Model1") --change to name of 1st model

for i, v in pairs(model1:GetDescendants()) do
	if v:IsA("MeshPart") or v:IsA("Part") then
		v.Touched:Connect(function(hit)
			if hit:FindFirstAncestor("Model2") then --change to name of 2nd model
				--do code
			end
		end)
	end
end

I made this for someone else not too long back. It checks if a part of some model is touching another part, and then checks if that other part belongs to a model of some given name.

I think one way to do this would be;

  1. Model:GetBoundingBox() to get the space the model occupies
  2. Use those values to create a region3 (by using the CFrame and Vector3 size to calculate the “corners” of the bounding box
  3. Use the region3 to do WorldRoot:FindPartsInRegion3

You may have to google some stuff (like the math for step 2 if you’re unfamiliar with creating a region3) but overall I think this could be a relatively simple solution. You can also visually “create” the bounding box to help with this step, which you can see an example of on this page; Model | Roblox Creator Documentation

Ninja edit: I probably would not use touched, get touching parts, etc. because in my experience it’s unreliable if your parts are anchored, cancollidefalse, moved via CFrame, etc.

Once I do that how do I check if one of the parts in that region does not belong to the original model?

Edit: Did some research and found “workspace:FindPartsInRegion3WithIgnoreList”
and it seems to be working! thanks

1 Like