Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

What's wrong with my kill brick?

Asked by
Restic 15
11 years ago
1function kill(victim)
2    victim.Parent.Humanoid.Health=0
3end
4 
5script.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?

3 answers

Log in to vote
2
Answered by
jav2612 180
11 years ago

Try this instead:

1function kill(victim)
2    if victim.Parent:FindFirstChild("Humanoid") ~= nil then
3        victim.Parent.Humanoid.Health = 0
4    end
5end
6 
7script.Parent.Touched:connect(kill)
0
Thanks. I am still confused why mine didn't work. Can you explain? Restic 15 — 11y
0
The script probably broke when a part that didn't have a Humanoid touched it. This script first checks if the part's parent has a humanoid before trying to change it so that if it encounters a part without a humanoid, it will not break jav2612 180 — 11y
0
WHY IS JAV SO GOOD? IS HE THE SAVIOUR THAT HAS COME TO HELP US?! Yeevivor4 155 — 11y
Ad
Log in to vote
1
Answered by
painzx3 43
11 years ago

Here's the more common version:

1function onTouched (part)
2    local h = part.Parent:findFirstChild("Humanoid")
3    if(h~=nil)then
4    h.Health = 0
5end
6end
7 
8script.Parent.Touched:connect(onTouch)

EDITED! Should work

Log in to vote
0
Answered by 10 years ago

This should work

1function onTouch(part)
2local humanoid = part.Parent:FindFirstChild("Humanoid")
3if (humanoid ~= nil) then -- if a humanoid exists, then
4humanoid.Health = 0 -- damage the humanoid
5end
6end
7script.Parent.Touched:connect(onTouch)

Answer this question