How to get the part name that the player touched

So I want to get the part name of the part that the player touched.
I tried it but instead it print character parts name instead of the part name

Here is the script:

local function Touched(Part)
	print(Part.Name)
	-- prints 
    -- Character Parts like Right Foot instead of a part name
end

Part.Touched:Connect(Touched)

Output:
image

Part is a variable for the part you want to touch. In the function, Part is replaced by a parameter. Try changing the name of the parameter

local function Touched(otherPart)

or

local function Touched(part) -- lowercase p

or any name you want except “Part” (capital P)

it still prints the character parts instead of the part name that player touched.

You have 2 variables named Part, they’re interferring with each other. Try changing the Touched functions parameter to Hit instead.

local function Touched(Hit)
	print(Part.Name) --prints the part touched
	print(Hit.Name) --prints the character part touched
end

Part.Touched:Connect(Touched)

I have multiple parts I’m using for i,v.

That doesn’t really affect anything, unless v is the part you’re using .Touched on, then you just print v.Name instead.

so the function will be inside for i,v?

If you’re using the for loop to make multiple parts be able to be touched, then yes.

Parts would be the parent of all your parts

for i,v in Parts:GetChildren()
    v.Touched:Connect(function(Hit)
        print(v.Name)
    end
end
2 Likes