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

Can someone explain to me how this works, I am confused on what hit is?

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

3 answers

Log in to vote
1
Answered by
oreoollie 649 Moderation Voter
5 years ago
Edited 5 years ago

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!

0
Thank you so much, I now understand how this code works. jack33887 9 — 5y
0
No problem! oreoollie 649 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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)
Log in to vote
0
Answered by
SCP774 191
5 years ago

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)

Answer this question