How to detect torso touching another part?

Hi, I’m trying to add a sound effect for when you touch something, but the problem with this script that I modified from someone is that is detects all body parts instead of just one, and I can’t figure out how to only detect one (Torso).

local part = script.Parent.Parent
local sound = script.Parent

part.Touched:Connect(function(itemTouchingPart)
	print("Item touching part: ", itemTouchingPart)

	if (itemTouchingPart.Parent and game.Players:FindFirstChild(itemTouchingPart.Parent.Name))then
		sound:Play()
		wait(2)
	end
end)

I’m still pretty new to Lua, so any help is appreciated.

1 Like

maybe check the touched part’s name

if itemTouchingPart.Name == "Torso" then
   -- code
end
1 Like

Well, for 1 the Sound will keep constantly playing cause there’s no cooldown that’s being implemented inside the Touched Event

I mean a simple thing to do would be is to just check for the name of the Part, and confirm that a Player touched it

local part = script.Parent.Parent
local sound = script.Parent
local SoundPlaying = false

sound.Ended:Connect(function()
    SoundPlaying = false
end)

part.Touched:Connect(function(itemTouchingPart)
	if itemTouchingPart.Name == "Torso" and game.Players:GetPlayerFromCharacter(itemTouchingPart.Parent) and SoundEnded == false then
        SoundEnded = true
        sound:Play()
    end
end)
2 Likes

I think the best thing to use here would be ‘IsA’ since this checks if it is a humanoid. If an exploiter changes any other part’s name to “Humanoid” that would get registered too so this is most convenient:

if itemTouchingPart:IsA("Humanoid")

@bradlovespoke

1 Like

Hey, sorry for late reply.

Since I couldn’t see “Torso” pop up in the debug menu, I tried changing the body part to “Head”, which works good enough for me. Thanks!

I finally understood what you were trying to say and this helped a lot, thanks!