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)
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)
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.