Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Is it possible to detect what part is touching the other part?

2 answers

Hello!

To answer your question in brevity, yes it is possible. The Touched event has a parameter (part in the snippet below) that refers to the other part touching the part in question.

BasePart.Touched:Connect(function(part)
	print(part.Name)
end)

You can read more about the Touched event here and see a working example here.

Another way to do it is by using the FindFirstChild() method (NOT a function; functions do not run under a class or object):

part = script.Parent
if part:FindFirstChild("Part") ~= nil then
	print("vaild Part")
end

Both the Touched event and the FindFirstChild() method contribute to something touching another thing.

Other things that can be touched/clicked

ClickDetectors have the event MouseClick to detect whenever the Parent of the ClickDetector is clicked. It is used as so:

part = script.Parent
part.ClickDetector.MouseClick:connect(function()
	print("Part has been clicked")
end)

In Guis, this event has two names: MouseButton1Down and MouseButton1Click. They both do the same thing whenever a Text or ImageButton is clicked, so it doesn't matter:

gui = script.Parent
--[[
1st example, which uses MouseButton1Down
This event is used to detect whenever a player left clicks on a Gui
--]]
gui.MouseButton1Down:connect(function()
	print("Valid Mouse Input: left Click")
end)
--[[
2nd example, this one uses MouseButton1Click
Again, they do the same thing, so don't be confused
--]]
gui.MouseButton1Click:connect(function()
	print("Gui has been clicked")
end)	

Now, there is something in Roblox called raycasting. Raycasting is when a ray touches a Part. I can't explain it without being complicated, so you can look for it on this site, the Wiki. or YouTube.

1 comment

0

I just put this in just in case you also wanted to now about how things are not just touched, but clicked as well.

This discussion is preserved as it was. New answers, comments, and votes are closed.