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
10 years ago
function kill(victim)
    victim.Parent.Humanoid.Health=0
end

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?

3 answers

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

Try this instead:

function kill(victim)
    if victim.Parent:FindFirstChild("Humanoid") ~= nil then
        victim.Parent.Humanoid.Health = 0
    end
end

script.Parent.Touched:connect(kill)
0
Thanks. I am still confused why mine didn't work. Can you explain? Restic 15 — 10y
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 — 10y
0
WHY IS JAV SO GOOD? IS HE THE SAVIOUR THAT HAS COME TO HELP US?! Yeevivor4 155 — 10y
Ad
Log in to vote
1
Answered by
painzx3 43
10 years ago

Here's the more common version:

function onTouched (part)
    local h = part.Parent:findFirstChild("Humanoid")
    if(h~=nil)then
    h.Health = 0
end
end

script.Parent.Touched:connect(onTouch)

EDITED! Should work

Log in to vote
0
Answered by 9 years ago

This should work

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)

Answer this question