script.Parent.Touched:connect(function(hit)
if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
hit.Parent.Humanoid.Health = 0
end
end)
Can someone explain to me how this works, I am confused on what hit is?
3 answers
This is a kill brick. Lets go over this code line by line. Line 1:
script.Parent.Touched:connect(function(hit)
This registers a listener with Roblox's engine. What this means is that Roblox will know that when script.Parent is touched, we need to run the function inside of the parenthesis. The hit argument is passed by the Touched event. When the part gets touched, hit becomes a reference to the part that touched it. Line 2:
if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
This line uses more code than necessary but, it checks to make sure the part that touched it exists, it checks to make sure the parent of the part that touched it exists and it checks to make sure there is a Humanoid located inside the parent of the part that touched it. You really only need to check for the Humanoid. If all of these checks succeed (return true) then the code inside the if statement will run, if the checks fail (return false or nil) then the code inside the if statement will be skipped Line 3:
hit.Parent.Humanoid.Health = 0
This changes the Humanoid's health to 0, obviously killing the character. I've wrote some more efficient code below.
script.Parent.Touched:Connect(function()
if hit.Parent:FindFirstChildOfClass("Humanoid") then
hit.Parent.Humanoid.Health = 0
end
end)
I hope my answer solved your problem! If it did, please remember to mark it as correct!
It’s a kill brick. When touched, the humanoid dies. It has deprecated code though, so I wouldn’t recommend using it. Its if statement is unnecessarily long, and just needs one thing from it.
script.Parent.Touched:Connect(function(part)
local WorkspaceOrChar = part.Parent
if WorkspaceOrChar:FindFirstChild'Humanoid' then
WorkspaceOrChar.Humanoid.Health = 0
end
end)
It's a brick that will set a character/mob/AI's Humanoid's health to 0 (Kill it/him/her). The hit refers to the part that touched the script's parent. (A part)
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
Thank you so much, I now understand how this code works.
No problem!