Touch events stops working if it touches anything else

I have this touch event that would damage the player when toched.The issue is that if the part with the touchevent touches something else before the player (such as its hat) it would not trigger. So what is going on here and is there any solution for this? I thank you in advance.

script.Parent.Touched:Connect(function(hit)
print(hit.Name)
local humanoid = hit.Parent:FindFirstChild("Humanoid")

	if humanoid then 	
	humanoid.Health = humanoid.Health - 10
	script:Destroy()
	end
end)
1 Like

Why are you destroying your script before you actually damage the player? That may be the issue.

2 Likes

it does damage the player i destroy the script so it doesn’t trigger more than once

1 Like

Scripts read commands in order, if you use script:Destroy() first it will not continue.

1 Like

it still triggers that’s not the issue. I just changed it. Still works in the same way.

local debounce = false

script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if not debounce and player then
		debounce = true
		player.Character.Humanoid.Health = player.Character.Humanoid.Health - 10
		wait(5)
		debounce = false
	end
end)

You should look for the player, instead of the humanoid (game.Players:GetPlayerFromCharacter(hit.Parent).

Also, I don’t know why you would remove the script. I would instead suggest a debounce, so a player can only get damaged once every 5 seconds (in this example)

1 Like
function onTouch(part) 
    local humanoid = part.Parent:FindFirstChild("Humanoid") 
    if (humanoid ~= nil) then	-- if a humanoid exists, then
            humanoid.Health = humanoid.Health - 10	-- damage the humanoid
	        script:Destroy()
    end 
end

script.Parent.Touched:connect(onTouch)

This script worked for me, I tested it with and without having it touch a hat. Both times it took away 10 health, and did not trigger again after that.

1 Like