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
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