Getting both parts w/ touched event

Is there an efficient way to get both parts involved in a touched event?
For example, if the same function were connected to two (or more) separate parts, would there be an easy to distinguish which of the connected parts is being touched? Maybe this is the wrong way to go about it. Perhaps it is possible to generate multiple functions that each contain a reference to their connected part? If so I don’t know how to do that.

Right now I’m just calculating the minimum distance between the otherPart and the connected parts to find which of the connected parts is closest to the otherPart…

I probably interpreted this wrong, but Touched is an event of a basePart, so you must already know what the first part is. As for the other part touching, it’s the first argument passed to the connected function. Here’s some sample code:

local mainPart = workspace.Part

mainPart.Touched:Connect(function(partThatTouched)

print(mainPart..' Touched '..partThatTouched)

end)
4 Likes

What are you using this for? It’s not clear quite what you’re doing here so it’s hard to provide code suggestions. However, you must know the two parts involved, because you had to call .Touched on one of the parts, and the other part is the first argument. e.g.

local Part1 = game.Workspace.Part1
Part1.Touched:Connect(function(Part2)
print(Part1)
print(Part2)
end)

I believe there is an event in humanoid if you are looking to see when a limb touches a part, this returns the part touched and the limb that touched it.

You’re right, there is an event that handles exactly that.

Usage:

local Humanoid = game.Players.LocalPlayer.Character.Humanoid
Humanoid.Touched:Connect(function(TouchingPart, HumanoidPart)
   print(TouchingPart.Name, "Touched", HumanoidPart.Name)
end)
3 Likes