How would I detect what part was touched with .Touched?

I need to find out how to do this, because with the gun I need to add the bullet script to detect what part was touched and if the part was a certain name to change one of its properties. Does anyone know the best way to detect the name of what was touched?

If you’re calling a part touched event, you know what part needs to be touched, correct?

The touched event has a parameter that is indexing what part was touched. You can use that to find the part’s name.

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

When you call the function with .Touched, give it a parameter with a name (such as part), which includes information about the part that touched it.

1 Like

yeah but how to I detect if it touched said part, and only if it was touched by a part with a certain name

That is pretty much it. Sorry if I misunderstood the question.

in this circumstance stance how would I use IsAncestorOf() to detect if what it was touching is the child of a folder in workspace?

Maybe something like this?

local Humanoid = -- humanoid location
local touchParts = workspace.Folder -- parts in folder

Humanoid.Touched:Connect(function(touch)
    if touch:IsDescendantOf(touchParts) then
        -- code
    end
end)
2 Likes

Actually, this looks like exactly what I need

hey! but do you know how I could know which part was actually touched and find it’s name? thanks!

I was very new when I made this post, but now this should solve your issue

part.Touched:Connect(function(hit)
// "hit" is the basePart that touched "part"
end)