So I am making a shop in which I check if the player is in a certain area. I can use gettouchingparts or findpartsinregion3 for both of these, but I have to code a little extra for findpartsinreigon3. What is the difference between these?
What is the difference between GetTouchingParts and FindPartsInRegion3?
The difference is,
:GetTouchingParts gets the touching parts for a specific part, with a caveat being it doesn’t account for parts that do not have collisions.
:FindPartsInRegion3, however, gets the parts in a specified
Region3, lets you specify a maximum part count, and even lets you specify a whitelist/ignore list with specific methods. And it does account for parts with collisions disabled.
So if I wanted just to detect a player in a region, would gettouchingparts be good for that?
Use FindPartsInRegion3; it might be a bit of extra code but it’ll probably work more consistently! Always good to learn new skills
FYI: You can make it detect parts even if the Part has CanCollide off with this simple trick:
That method is hacky, and thus should not be relied upon. We should not depend on hacks just for some behaviour that should be built-in!
Not a hack, it is documented that it is because of the TouchInterest:
You cannot efficiently use FindPartsInRegion3 in place of GetTouchingParts for non-primitive objects (i.e. custom meshes) unless you manually build in overlap checks for all the shapes you will have in your game yourself, and that is extremely non-trivial.
OP should feel free to use GetTouchingParts with empty touched handler trick for convenience – it is a documented effect and much more time-efficient than needing to know the complicated math to write your own, and with GetTouchingParts it works out-of-the-box for all shapes.
Could you clarify what you mean by this. Other than that I understand your post
Region3s are rectangular and not rotated. You will need to do the following steps to make FindPartsInRegion3 work like GetTouchingParts:
- Find the bounds that you need to check in from the rotated origin part (i.e. calculate bounding box and make a Region3 that captures the entire box)
- Get all the parts inside that box with FindPartsInRegion3
- Now you need to calculate for each part that it actually intersects with the shape
- For each part in the result, you have to check that one of its edges intersects with the volume you are looking for touches in
For beginner programmers, the math required for #3 is already really hard to grasp when your part is a regular part (i.e. a sphere or a box). It’s even harder when your mesh is a non-box convex shape (i.e. wedge, cylinder, cone), and exceptionally hard for concave shapes (shapes with holes, hollow areas / indents).
Also, manually doing the math yourself doesn’t work unless you put the mesh data of each possible mesh in your game into a Lua script somewhere, since you cannot read mesh data in-engine.
That’s why I recommend just using GetTouchingParts for it. It’s the difference of 1-2 days of work vs 5 minutes of work to cover all possible objects you might want to use as a hitbox for GetTouchingParts.