Quick hack for GetTouchingParts with CanCollide = false

I wanted to do hit detection via GetTouchingParts but I noted that this function abides by physical simulation rules, meaning that CanCollide=false parts will not register a hit unless they have a TouchInterest. This was a problem that was a slight hindrance to my work. Luckily, I thought of a simple script that remedied the issue:

Click to show script:
local Con_A, Con_B
if not PartA.CanCollide then
    Con_A = PartA.Touched:Connect(function () end)
end
--(repeat for PartB, using variable Con_B)
local TouchingParts = PartA:GetTouchingParts()
local Touching = false
for Index, Other in pairs(TouchingParts) do
    if Other == PartB then
        Touching = true
        break
    end
end
if Con_A then Con_A:Disconnect() end
if Con_B then Con_B:Disconnect() end
--Do something with Touching

Personally I had doubts of this working due to the 60Hz physics update rate, but it works really well and in fact, to what I know, works better because of that update rate. The parts’ TouchInterests are (ideally) created then invalidated during one physics step so the update doesn’t require two frames (one frame to listen to touches, one frame to invalidate the listener).

API for this would be awesome for QOL but is otherwise unnecessary. Could probably just write a ModuleScript for this anyway.

6 Likes