How To Detect If Part is Touched

How to detect if part is touched by another part using local script

There are a lot of ways of doing this. But, the best approach is to use: workspace:GetPartsInPart(BasePart).

Here is the documentation: WorldRoot | Documentation - Roblox Creator Hub

Like so:

local Script,Workspace= script,workspace

local Part = Script.Parent

local Parameters = OverlapParams.new()
Parameters.FilterDescendantInstances = {Part}

local function PartCollided()
	return Workspace:GetPartsInPart(Part,Parameters)
end

print(PartCollided())

if PartCollided() then
	print(Part.Name.." had a collision with another part.")
else
	warn(Part.Name.." had no collision with another part.")
end
1 Like

This should help: Google Search

It’s the same for LocalScripts and Scripts, use part.Touched.

This is a lot like cloud_cat script other than it will handle the returned array from GetPartsInPart.

local Script, Workspace = script, workspace
local Part = Script.Parent

local Parameters = OverlapParams.new()
Parameters.FilterDescendantInstances = {Part}

local function PartCollided()
    local parts = Workspace:GetPartsInPart(Part, Parameters)
    return #parts > 0, parts
end

local collided, touchingParts = PartCollided()

if collided then
    print(Part.Name .. " had a collision with another part.", touchingParts)
else
    warn(Part.Name .. " had no collision with another part.")
end

Part.Touched only works when one of the parts is unanchored, that’s why touched events don’t register other anchored parts, but detects the character which is unanchored, the best way is Part:GetPartsInPart() as others have mentioned.

1 Like

Yes, so? Aren’t the parts going to be unanchored?

Depends on the use case, but as others have stated, Part:GetPartsInPart() is just better and more reliable because its more consistent than Part.Touched imo.

If you take a projectile system, raycasting would be better than an actual part with a part.Touched, but again depends on use case.

It also happens to be a lot more laggy if we’re talking optimization.

Thats just not true, the Touched event relies on physics simulations to detect parts, which is a lot less optimised than one direct check with Part:GetPartsInPart() which doesn’t rely on any physics. So no, its not more laggy, its quite the opposite.

thank you so much!, i got helped by this reply, now i can make my game better.