How to check if part is touching another part

Hello

Is it possible to check if part A is touching part B?

I tried :GetTouchingParts() but it doesn’t work:
My script:

a = script.Parent.a
b = script.Parent.b

for i,v in pairs(a:GetTouchingParts()) do
	print(v)
end

image
No output :frowning:

Couldn’t you just do a touched function instead…? Unless if that’s not what you want

local Part = script.Parent
Part.Touched:Connect(function(Hit)
    if Hit.Name == "Part B" then
        --Do something here
    end
end)
1 Like

But is it possible to get these parts later (after event)?

You could try using the .Touched event and adding a if statement to check names or properties, I would try doing this (if you named the parts accordingly)

--If the script was a child of Part A
script.Parent.Touched(function(b)
    if b.Name == "PartB" then
        print("Part A has touched Part B")
end)

Make an invisible part thats slightly bigger than the original and run GetTouchingParts on that. Note: If the part has cancollide set to false, then you will need to create a touched connection then disconnect it, e.g.
local touched = part.Touched:Connect(function() end) touched:Disconnect() View the wiki page here.

2 Likes

It worked, thanks!
Working script:

a = script.Parent.a
b = script.Parent.b
c = Instance.new("Part")
c.Position = a.Position
c.Size = a.Size + Vector3.new(0.5,0.5,0.5)
c.Anchored = true
c.Transparency = 1
c.Parent = workspace

for i,v in pairs(c:GetTouchingParts()) do
	print(v)
end