Deleting a Part If 2 Parts Are Touching

I was looking on youtube, but I couldn’t find out how to check to see if 2 parts are touching each other. How would I write a script that checks if 2 parts are touching each other?
Thanks.

EDIT:

local Red = game.Workspace.Red
local RedPart = game.Workspace.RedPart

game.Workspace.Red.Touched:Connect(function(partTouching)
if RedPart == Red then Red.Remove() end
end)

Why isn’t this working?

1 Like

Maybe this will help you.

Hope I helped!

1 Like

attach a .Touched connection to one of the parts

part1.Touched:Connect(function(partTouching)
    if part1 == part2 then print("Part 1 touched Part 2") end
end)

make sure to disconnect the event when you’re done to stop memory leaking

note this solution only works when the parts are CanCollide true and they are using physics. this solution will also fire many times, so the use of a debounce is highly recommened (a tutorial for that exists on the forum somewhere)

if this solution doesn’t work for you, the one suggested by @LargeMakesStuff probably will

1 Like

You should do something like this.

local touchingParts = part1:GetTouchingParts()

for i, v in pairs(touchingParts) do
if v == part2 then
--What you want to happen if part2 is touching part1
end
end

GetTouchingParts() returns an array (List) containing all the parts that are touching part1.

In pairs sorts through the list that is in the parenthesis, in this case it is touchingParts. The variable right before the “In Pairs” (I named it “v”), represents the value in the list it is in middle of sorting through.

The if v == part2 then means if v is part2, then run the commands.

I don’t know if I explained it that well, but hope it helps!

3 Likes

You would probably, have to add a local part = script.parent to that one or else the script would not recognize the part1.

Yeah, I just assumed he would do that.

There are two methods that I can think of off the top of my head.

.Touched and .TouchEnded


local ArePartsTouching = false

local PartA = workspace.PartA
local PartB = workspace.PartB

PartA.Touched:Connect(function(HitPart)
  if HitPart == PartB then ArePartsTouching = true end
end)
PartA.TouchEnded:Connect(function(HitPart)
  if HitPart == PartB then ArePartsTouching = false end
end)

This method leaves you with a variable to check when needed.

The issue with this method is if they start out as touching, it won’t register that. Also, it will always be updating the variable even if you’re not checking it at the time.

:GetTouchingParts()

local function ArePartsTouching(PartA, PartB)
  local TouchingParts = PartA:GetTouchingParts()
  for _, p in pairs(TouchingParts) do
    if p == PartB then return true end
  end
  return false
end

This method is a function to check when needed.

The issue with this method is if there are many TouchingParts, it could be pretty slow. It only checks when you actually call it, so it doesn’t have the previous method’s flaw.

2 Likes

I wouldn’t rely on Touched and TouchEnded, for me they’re really buggy. (Sometimes TouchEnded doesn’t fire, sometimes Touched and TouchEnded will fire multiple times before the other fires, etc) It also wont work if the parts are moved into each other by something other than phsyics.

Using GetTouchingParts you might want to use table.find instead and create a temporary touched connection so it works with collisions disabled parts.

local function blank()end
local function isTouching(p1,p2)
	local con = p1.Touched:Connect(blank)
	local result = table.find(p1:GetTouchingParts(),p2) ~= nil
	con:Disconnect()
	return result
end
2 Likes

By any chance do you know why this isn’t working?

local Red = game.Workspace.Red
local RedPart = game.Workspace.RedPart

game.Workspace.Red.Touched:Connect(function(partTouching)
if RedPart == Red then Red.Remove() end
end)

small typo.

instead of RedPart == Red try partTouching == RedPart
also, do Red:Destroy() instead of Red.Remove()

Besides for what infranova said, why would you make a variable called red if your not going to use it? Also, .Touched will destroy it the second the parts touch. If you want the part to be destroyed if it is in contact with another part at a certain point, then you should look at my reply above. Another thing, you should check the output for errors…

So I did this, but it doesn’t work what am I doing wrong?

local touchingParts = game.Workspace.Red:GetTouchingParts()
local Red = game.Workspace.Red
local RedPart = game.Workspace.RedPart

for i, v in pairs(touchingParts) do
if v == RedPart then
	Red.Destroy()
end
end

Turn the period before “Destroy” into a colon.

It still doesnt work and there are no output errors.

The script you’re trying only gets the touching parts at run-time (which they won’t be touching at that time.)
If you want to delete a part whenever they touch, the simplest way would be to use a .Touched event (as in your post.)

Keep in mind, this doesn’t work for Anchored parts. If your parts are unanchored, this should work fine though.
The code in your post should work, however you used an incorrect variable for your if statement.

local Red = game.Workspace.Red
local RedPart = game.Workspace.RedPart

game.Workspace.Red.Touched:Connect(function(partTouching)
	if RedPart == partTouching then -- changed Red to partTouching on this line.
		Red.Remove()
	end
end)
``
2 Likes