1 | function kill(victim) |
2 | victim.Parent.Humanoid.Health = 0 |
3 | end |
4 |
5 | script.Parent.Touched:connect(kill) |
My question is why it kills only one time then says "Disconnected event because of exception." Is it because there's not an if-then statement in there?
Try this instead:
1 | function kill(victim) |
2 | if victim.Parent:FindFirstChild( "Humanoid" ) ~ = nil then |
3 | victim.Parent.Humanoid.Health = 0 |
4 | end |
5 | end |
6 |
7 | script.Parent.Touched:connect(kill) |
Here's the more common version:
1 | function onTouched (part) |
2 | local h = part.Parent:findFirstChild( "Humanoid" ) |
3 | if (h~ = nil ) then |
4 | h.Health = 0 |
5 | end |
6 | end |
7 |
8 | script.Parent.Touched:connect(onTouch) |
EDITED! Should work
This should work
1 | function onTouch(part) |
2 | local humanoid = part.Parent:FindFirstChild( "Humanoid" ) |
3 | if (humanoid ~ = nil ) then -- if a humanoid exists, then |
4 | humanoid.Health = 0 -- damage the humanoid |
5 | end |
6 | end |
7 | script.Parent.Touched:connect(onTouch) |