Instantly Kill upon touching a brick

Hello! Recently, I’ve been having an issue with making a kill block. I know it should be simple, but my script just doesn’t work!

Here’s the script:

	local h = Obj.Parent:FindFirstChild("Humanoid")
	if h then
		h.Health = 0
	end
end

script.Parent.Touched:Connect(onTouched)
-- 

Can anyone spot the problem? I can’t find it.

1 Like

Is that the whole script? It looks like you missed some code at the top.

local function onTouched(Obj) --This line was missing
	local h = Obj.Parent:FindFirstChild("Humanoid")
	if h then
		h.Health = 0
	end
end

script.Parent.Touched:Connect(onTouched)
1 Like

Ok, let me do that. I’m heading into Roblox Studio to see if it works.

EDIT:
Nope, still doesn’t.

Could you show the explorer window? Since I just tested this script and it worked as expected.

You need to parent the script to the part you want to be the kill brick for the code provided to work.

I did. I know that much about scripting lol

Wait, I’ll try it one more time.
EDIT:
Yay it worked! Thanks!!!

script.Parent.Touched:connect(function(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
		hit.Parent.Humanoid.Health = 0
	end
end)

I’ll try that one next time, thanks!

I have two things to point out:

  1. Hit will always be an Instance and its Parent will always be a descendant of workspace; otherwise, the function would have never fired.
  2. Save what the :FindFirstChild() returns in a variable, don’t throw it away.

Setting a Humanoid’s health to 0 is a bad practice. Instead, use h:TakeDamage(DamageAmount).

Okay, I will keep this in mind. Thanks!

This topic is already solved but I just wanna put this short script out there.

script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then --if the script finds a humanoid inside hit.Parent then it will run the next line of code
hit.Parent:BreakJoints() --also kills the player
end
end)