Simple trick to make GetTouchingParts work with non-CanCollide parts

NOTE: This is obsolete in favor of better bounding box checking API now: https://devforum.roblox.com/t/introducing-overlapparams-new-spatial-query-api/1435720

Suppose you have PartA and PartB in workspace that intersect, but both are non-CanCollide (or only one of them is non-CanCollide):

image

You might know that the following call will not yield any results in that case:

local results = workspace.PartA:GetTouchingParts()
print(#results) --> 0

However, if you attach an empty Touched handler before the call and disconnect it afterwards, all intersecting parts are returned:

local function GetTouchingParts(part)
   local connection = part.Touched:Connect(function() end)
   local results = part:GetTouchingParts()
   connection:Disconnect()
   return results
end

local results = GetTouchingParts(workspace.PartA)
print(#results) --> 1

This is presumably because, due to the Touched handler, the part starts participating in physics differently, which allows for the non-CanCollide parts that intersect itself to be found.

I figured I’d stick this here in case someone needs this functionality but doesn’t know this trick yet either.

Credit to @evaera for telling me about it

453 Likes
Fastest way of detecting whether two parts touch each other?
Using GetTouchingParts while CanCollide = false
How to tell if 2 parts are on the same spot?
TouchedEvent dont work
How do you detect parts around another part with a certain radius?
How to check if a bullet is shot close to a player's head?
Identifying overlapping bricks
Would multiple :FindPartsInRegion3 lag if it were checking every 1 second?
:GetTouchingParts() doesn’t detect touching parts while part is moving
Decent Hit Detection Methods
Introducing OverlapParams - New Spatial Query API
What is the difference between GetTouchingParts and FindPartsInRegion3?
The code I wrote wont detect object placement collisions
Is it even possible to have an accurate weapon hitbox that grabs all touching parts (including noncollide)?
Best way to detect area collisions?
Most Efficient and Performance wise method for Cube-like Hitboxes?
Zone+ v1 (deprecated) | Retrieving players within an area/zone
How can I make a block building system that can stack on top of eachother?
Detecting Certain Parts in a Sphere
PSA: Using TouchEvents Properly!
If Player is in an area
If Player is in an area
If Player is in an area
:GetTouchingParts() bug
Collision on Placement Script works rarely?
How do I determine if two anchored parts are intersecting?
OnTouch and TouchEnded help
How can i trigger a touched without having to move
Simple and fast 2D Raycast Renderer
Does :GetTouchingParts() work when using tweens?
How would I optimize this?
Does :GetTouchingParts() not get the character?
Zone+ v1 (deprecated) | Retrieving players within an area/zone
Melee Hitboxes - What Is The Best Solution, and is the Touched event really that bad?
GetTouchingParts not working
Detect if a part is in another part
{still no fix :*} Placement system - collision script not working properly?
Part.Touched event in front of GetTouchingParts makes the table more efficient or something like this for a reason, why is this happening?
BasePart:GetTouchingParts() for any parts, not just CanCollide true
The touched event fire before hitting anything
How to getting start hitbox system with region3?
Get touching parts not working
How to get a list of object at a specified position?
Parts not interacting with .Touched
GetTouching part not registering part
New Part Collision Property: CanTouch [ACTIVATED]
Tips on how to make a breath ability that can freeze people
Script won't work unless a function that does literally nothing is in the script
How detect Part:GetTouchingParts() if Part CanCollide == false?
Alternative to Region3?
Put touched event in a if statement
How can I check how many parts are touching a certain part?
Why is :GetTouchingParts() not working on children of my NPC but working on another NPC?
How to make advanced flooding system?
Any other ways to check if a player is touching a part instead of using .Touched?
Check if a build is touching the baseplate, or if another build is touching a build that is touching the baseplate?
:GetTouchingParts() not detecting character parts
Rotated Region 3 Module
Checking players current room
Alternative for Touched event?
isTakingSpace function not working
Can someone introduce me to the different ways of moving a projectile?
Is there such a thing as good square shaped hitbox detection for combat?
How do i make a zone where when in it gives you sword
How can I detect if a part is surrounded by other parts?
How to detect a swimming part?
Why Isn't :GetTouchingParts() registering anything?

That’s a really interesting and clever bypass to this annoying issue.

14 Likes

We shouldn’t even have to do this :confused:
I wish there was an official implementation of this feature for all parts, no matter what their rotation and collisions are
Nice find though!

31 Likes

Super useful! This works because it adds a TouchInterest object to the part. The GetTouchingParts method looks for either can-collide parts or parts with TouchInterest.

Documented on the GetTouchingParts() page:

30 Likes

I agree, but I believe that it’s because of how the physic engine works;

So when a part is CanCollide = false the physic engine will not register any TouchEvents or Detects Touch cause it doesn’t have to, and if it did then it would eat up more memory (as equal to a Part with CanCollide = true) then it would with it’s current behavior, when a part is CanCollide = false it’s not physically there, that’s why you can walk right through it.

Yes it would be nice if we don’t have to do all of that, as it should be done internally.

7 Likes

This isn’t true…any part with a TouchTransmitter instance inside will fire Touched events. I have tens of CanCollide false parts with Touched events that work perfectly fine.

7 Likes

I wish GetTouchingParts() took a parameter such as GetTouchingParts(bool IgnoreNonCanCollide = true). This way, we can specify. It would default to true for backwards compatibility in case any older games rely on the current behaviour.

Another way to do this would be to return all the parts and have the developer write a for loop and do an if statement for collisions if they really don’t want to act on non CanCollide parts. I’d be all for just returning all parts (CanCollide off and CanCollide on).

22 Likes

Thank you, really useful!

After all these years we had a solution all the time…

This seems like a hack. I’d be careful relying on this behavior.
In an ideal world, GetTouchingParts would just work regardless if there’s a TouchTransmitter involved.

6 Likes

If it is a hack, then the Developer Hub documentation should probably be adjusted. It currently insinuates that it is fine (or even intended maybe) that parts with TouchTransmitters (=Touched handlers connected to it) are picked up.

8 Likes

I mean, it is intended behavior and I’m not saying you shouldn’t use it, just be cautious is all. I personally think this shouldn’t be a necessity to make the function work with non-colliding parts.

18 Likes

Huh, this is a really useful thing to know. I feel like roblox needs to official way to overcome this without using some hacky technique.

3 Likes

I have been using this forever, just want to come back and say thank you for discovering this :slightly_smiling_face:

4 Likes

This provided to be very useful, still to this very day. Thank you for this helpful solution.

Old post but most helpful thing ever known

2 Likes

This is obsolete in favor of better bounding box checking API now:

We almost reached the 3 year mark! Glad that’s over.

20 Likes