script.Parent.Touched:Connect(function(hit) if hit.Parent:FindFirstChild ("Humanoid")then hit.Parent:BreakJoints() end end
function onTouch(part) local humanoid = part.Parent:FindFirstChild("Humanoid") if (humanoid ~= nil) then -- if a humanoid exists, then humanoid.Health = 0 -- damage the humanoid end end script.Parent.Touched:connect(onTouch)
This should work:
script.Parent.Touched:connect(function(hit) if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then hit.Parent.Humanoid.Health = 0 end end)
Please contact me when this is not what you ment or if it doesn't work.
Since you only gave info in your title, I can only go off that. But, for what the title is asking, this is what I do:
script.Parent.Touched:Connect(function(hit) local Char = hit:FindFirstAncestorOfClass("Model") Char.Humanoid.Health = Char.Humanoid.Health - Char.Humanoid.Health end
Obviously, this wouldn't work if you put another model in-between the character and hit. But the reason I do this, is because if an accessory touched the part, hit.Parent wouldn't work. So this way, it checks for the first model that hit is in.