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

What is the function's parameter's parent?

Asked by 4 years ago

I am having trouble understanding what "hit"s parent is.

function Touched(hit)
    local kill = hit.Parent
    local humanoid = kill:FindFirstChildWhichIsA("Humanoid")
    if humanoid then
        humanoid.Health = 0
    end
end

script.Parent.Touched:Connect(Touched)

1 answer

Log in to vote
0
Answered by 4 years ago

The function :Touched() returns the OBJECT that touched the part. However, it doesn't returns a MODEL. So when a player's character touches the part, the :Touched() function will return a PART of the character. It will not return in character, because the character is a model. At the line 2 of the script you have wrote gets the player's character into a variable (the hit part's parent). But we don't know if the hit part's parent is a character of a player. Since every player's character has a child named humanoid, we can find out if it's a player's character or not. At line 3 you got the child named "humanoid" of the character into a variable. Then, at line 4, you used a if statement that verifies if the variable at line 3 EXISTS. If it exists, it's a player's character. Then, at line 5 you set the humanoid's health to 0. Hope this helped!

0
I just started scripting and I think I fully understand this now!!! NiceDerpling 14 — 4y
Ad

Answer this question