Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

What's wrong with my kill brick?

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

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)

3 comments

0

Thanks. I am still confused why mine didn't work. Can you explain?

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

0

WHY IS JAV SO GOOD? IS HE THE SAVIOUR THAT HAS COME TO HELP US?!

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

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.