How do I check if two parts are touching each other?

Hi Other Developers,

I wanted to check if two parts (Both are anchored with CanCollide off) are touching eachother, but don’t know how.

Thanks!

3 Likes

You can use :GetPartsInPart.

Code:

local partOne = workspace.PartOne
local partTwo = workspace.PartTwo

local overlapParams = OverlapParams.new()
overlapParams.FilterType = Enum.RaycastFilterType.Include
overlapParams.FilterDescendantsInstances = {partTwo}

if #workspace:GetPartsInPart(partOne, overlapParams) > 0 then
	print("Parts touching")
else
	print("Not touching")
end
8 Likes

thank you, another question though:
is there any way to continuously check if the parts are touching? Like putting it into a loop or connecting a function to it?

Put it inside a heartbeat loop, I use heartbeats since I heard they are good for performance too, idk tho.

2 Likes

This is an extremely bad idea.

Heartbeat in general is okay for performance, if you absolutely must do something repeatedly.
Calling GetPartsInPart inside a heartbeat function would decimate your performance.

2 Likes

works fine for me, replicates perfectly from client to server and multiplayer testing works flawless, on a normal mac m1.

2 Likes

You could use BasePart.Touched and BasePart.TouchEnded to change a variable if the touching part is the part you are looking for, and just read from that variable when you need to check if they are touching

2 Likes

Unfortunately, Touched and TouchEnded will only fire if either of the parts are unanchored.

2 Likes

It works, but it is horrible for performance, especially if you need to check multiple parts.

2 Likes

Ah right, sorry, I didn’t see that it says both parts are anchored.

In that case op can just run the function to check each time the two parts’ CFrame or Size property changes instead of every heartbeat

2 Likes

Performance is good :slight_smile: heres a video (not only a constant partinpart check, but also a gettouchingparts for terrain, hehe

1 Like

The video you sent does not show any kind of benchmarking or any statistics. That video only shows that your computer can run it on a single part at above 60 fps.

2 Likes

The system is heavier on the client than the server, client memory usage never surpasses 850 mb but I’ve not tested it on normal roblox only in studio so it probably varies but it’s stable and code is optimized to run well (except the parts where I do the part checks in a heartbeat), even works on Iphone x with 4 players in the game. But yeah Beans probably should search for a better solution.

2 Likes

It’s not really horrible for performance unless you do it with a lot of parts. But one or ten checks per stepped shouldn’t be noticeable.

2 Likes

No, it would actually perform extremely well, especially because of the OverlapParams I added.

For 100,000 iterations, it only takes 0.1 seconds. Think about how fast it would be for a reasonable amount of iterations like what you would do in a game.

Code:

local partOne = workspace.PartOne
local partTwo = workspace.PartTwo

local overlapParams = OverlapParams.new()
overlapParams.FilterType = Enum.RaycastFilterType.Include
overlapParams.FilterDescendantsInstances = {partTwo}

local startingTime = os.clock()

for i = 1, 100000 do
	if #workspace:GetPartsInPart(partOne, overlapParams) > 0 then end
end

print(`time elapsed: {os.clock() - startingTime}`) -- on average: 0.116311909999
1 Like

Yes, you would do this:

local partOne = workspace.PartOne
local partTwo = workspace.PartTwo

local overlapParams = OverlapParams.new()
overlapParams.FilterType = Enum.RaycastFilterType.Include
overlapParams.FilterDescendantsInstances = {partTwo}

while task.wait() do
	if #workspace:GetPartsInPart(partOne, overlapParams) > 0 then
		print("Parts touching")
	else
		print("Not touching")
	end
end

Make sure to set up your variables outside the loop to maximize performance.

2 Likes

a quick question: how would you check if Mutiple parts are touching?
thanks.

Like if multiple parts are all touching each other at the same time?

Yes, i have no idea how to do that,
thanks.

Bit more tricky but you can do this:

local partOne = workspace.PartOne

local partsList = {
	workspace.PartTwo,
	workspace.PartThree,
	workspace.PartFour
}

local overlapParams = OverlapParams.new()
overlapParams.FilterType = Enum.RaycastFilterType.Include
overlapParams.FilterDescendantsInstances = {partsList}

while task.wait() do
	if #workspace:GetPartsInPart(partOne, overlapParams) == #partsList then
		print("Parts touching")
	else
		print("Not touching")
	end
end
1 Like