How do I determine if two anchored parts are intersecting?

Hi. I’m trying to figure out how to determine if two anchored parts are intersecting. The touched event doesn’t work for my scenario because both of the parts are anchored.

Anyone know a solution?

You can call GetTouchingParts() on one of the parts and check if the result contains the second part.

local function arePartsTouching(part1, part2)
	local ti = part1.Touched:Connect(function() end) -- insurance
	local parts = part1:GetTouchingParts()
	ti:Disconnect()
	return (table.find(parts, part2) ~= nil)
end
4 Likes

Hey @blokav!

Your answer is super cool, but I don’t really understand it too well.
Could you explain why creating a touched event on part1 is insurance please?

Thank you!

part1.Touched:Connect will wait until a part touched it, maybe change from :Connect to :Wait()

this will wait until the two parts touch so if you dont need that you can use Connect

local function arePartsTouching(part1, part2)
    part1.Touched:Wait()
    local parts = part1:GetTouchingParts()
    retrun (table.find(parts, part2) ~= nil)
end

@RepValor
Actually I attached an empty function to the Touched event because GetTouchingParts will return an empty array if the part isn’t collideable unless the part has a TouchInterest in it, i.e. something is listening for a Touched event.

So the temporary touched event listener will ensure that any intersecting parts are detected in the case that part1 has CanCollide disabled.

This community guide basically gives the same solution:

5 Likes