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

How Do I Make a Part Kill a Player When It Touches The Player's Head?

Asked by 9 years ago

How do I make a part kill a player when it touches the player's head? As you can see, I am a beginner at scripting. I tried many ways to make this work. But they all failed. This one is the best I scripted.

function onTouched(hit)
    local Health = hit.Parent.Humanoid.Health
    if hit.Head then
        Health = 0
    end
end

1 answer

Log in to vote
0
Answered by
Redbullusa 1580 Moderation Voter
9 years ago
function onTouched(hit) -- "hit" is whatever your part touches.
    local Humanoid = hit and hit.Parent:FindFirstChild("Humanoid")
    local Head = hit and hit.Parent:FindFirstChild("Head")
    if Humanoid and Head and Head:IsA("BasePart") then
        Humanoid.Health = 0
    end
end
  • :FindFirstChild() - Finds the object you're looking for; returns a boolean value, indicating its existence.

  • :IsA - Decides whether the object is the given class name you want it to be; returns a boolean value, indicating if the object is the class name you're looking for.

Take note that an object's property can not be adjusted directly:

local Health = hit.Parent.Humanoid.Health
--
    Health = 0

You're just setting the value to 0, not the property. As you can see from the function I've edited, I changed the property by the Humanoid object variable, rather than the value.

local Humanoid = hit and hit.Parent:FindFirstChild('Humanoid")
--
    Humanoid.Health = 0
0
Thanks! I am smarter now because of you xD GatitosMansion 187 — 9y
Ad

Answer this question