Detect if a part touches another part

I’m working on a puzzle game, and one of the main components is rolling a cube onto a button to open a door. How would I make the button detect that the cube has touched it?

9 Likes

Put a Touched event within the button, when it fires, check if the object which touched the button was the cube.

4 Likes
script.Parent.Touched:connect(function(hit)
  if hit.Name == "Change this to the name of the cube" then -- change this
  --script to open the door
end
end)

Put this script inside of the button, and change that part inside the quotation marks.

20 Likes

I don’t know how I didn’t think of this lol, but hey, at least it works. Thanks!

5 Likes

This is quite simple to do, all you must do is use the .Touched event.

Such like this:

script.Parent.Touched:Connect(function(hit)
if hit.Name == "CubePart" then
doorpart.Transparency = 1
doorpart.CanCollide = true
end
end)
4 Likes

What would I do to make it so that a function occurs when the part is no longer touching the other part?

1 Like

Use touchended, so it fires when the part is no longer being touched

4 Likes

What if you want it to be for any part that you dont know what will touch it?

2 Likes

sorry, I’m a bit late, but here is a script you can use to detect parts that are touching another.

local part = script.Parent

part.Touched:Connect(Function(TouchingPart)
print(TouchingPart.Name)
end)

I know this is about the same as the other code but for specification TouchingPart, or Hit, is the part touching the Main Part and its name will be printed each time it hits the main part it will also be printed each time the Touching part changes CFrame. The same would happen for TouchEnded except it is for each time the part is no longer touching.

here is a script for touchended.

local part = script.Parent

part.TouchEnded:Connect(Function(TouchingPart)
print(TouchingPart.Name)
end)

also you may find an issue with no touching cooldown so in that case, here is some code to use.

local part = script.Parent
local debounce = false

part.Touched:Connect(Function(TouchingPart)
if debounce == false then
debounce = true
print(TouchingPart.Name)
end
wait(1)
debounce = false
end)
2 Likes

Continuando a discussão de Detect if a part touches another part:

Bro i think that im very very late but the real those scripts detect the player too ‘-’, use :GetTouchingParts() like this, remember its SCRIPT not local script

local part = script.Parent
while task.wait() do

local touching_parts = part:GetTouchingParts()

here will detect if the part get touch by other part

if #touching_parts ~=0 then

  local function whatwilldowhentouch()  --- function
  	 --- script
  	task.wait(0.5)
  end
  
  whatwilldowhentouch() --- run the function

end

end

if the script not work soo go to this link

:>