How do you make a part that kills you when a certain body part touches it? I want to make an obby where you can go a bit under the water, but if the water touches your head, you die.
Kill when a certain body part touches?
1 answer
Ahhhhh, didn't you try at least? To think a bit about, I'm sure if you have at least a knowledge of the basics of rbxlua youll know how to do this yourself.. But i guess i'll help.
So first, how to check if a certain bodyparts touches your part, well easy, you gotta use Touched event, it will fire when another object touches it. And to check what is the the touching part we gotta use the given paramater which is the part that touched the part that the event is connected to.
local part = script.Parent -- or whereever your part is
part.Touched:Connect(function(hit) <-- thats the paramater, we named it "hit" to make things clear, but you can name it whatever youw ant
print(hit)
end)
now that will print either LeftLeg or RightLeg (thats for R6) since logiclly those are the parts touching, but since this an under water thing i would think that the water is deeper than the player so it will also print Head if it touched. (im not even sure if youre using custom terrain..)
Now, we can't do Head.Health = 0 since thats not the property of Head. Remember that each player has a humanoid inside him that has health, speed... So to refer to the Humanoid we just do Head.Parent:FindFirstChild("Humanoid"), now it will error if the part that we are refering to isn't the humanoid, so we gotta add an if statment
And this should be your script
local part = script.Parent
part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent:FindFirstChild("Humanoid").Health = 0
end
end)
But we want the head to be touched right? not any part, so we gotta adda nother if statment
local part = script.Parent
part.Touched:Connect(function(hit)
if hit.Name = "Head"
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent:FindFirstChild("Humanoid").Health = 0
end
end
end)
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
Do you have an attempt?