Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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

1 comment

0

Thanks! I am smarter now because of you xD

This discussion is preserved as it was. New answers, comments, and votes are closed.