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

How to kill a player when I click a part?

Asked by
Xyternal 247 Moderation Voter
3 years ago

So following my first question, I did some scripting tutorials and leaned a bit of scripting. What I'm trying to achieve here is that when people click the part, they instantly die. I have added the script and click detector inside the part. My code is below.

01local part = script.Parent
02 
03local function kill(otherpart)
04    local partparent = otherpart.parent
05    local humanoid = partparent:FindFirstChild("Humanoid")
06    if humanoid then
07        humanoid.Health = 0
08    end
09end
10 
11 
12part.MouseClick:connect(kill)

Please tell me where I went wrong. Thank you!

0
Those of you who will reference me to peasfactory, note that I have made this code inspired by his tutorial. Xyternal 247 — 3y
0
You need a click detector for mouseclick. it would be part.ClickDetector.MouseClick:Connect(kill) BulletproofVast 1033 — 3y
0
otherpart should be the player who clicked it? BulletproofVast 1033 — 3y
0
So I tried it with the click detector, and it didn't work. And I'm sorry, but I don't understand your second comment. Xyternal 247 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

Hey the mistake you made was that "otherpart" is not equal to a part of the player but is equal to game.Players[playername] meaning that otherpart.Parent will be equal to game.Players. Try this script i made that should work :)

1local part = script.Parent
2 
3part.ClickDetector.MouseClick:Connect(function(plr) -- An event that activates a function when the part is clicked.
4    local humanoid = plr.Character:FindFirstChild("Humanoid") -- Finds the players humanoid
5    if humanoid then -- Checks if the player has a humanoid.
6        humanoid.Health = 0 -- Kills the player
7    end
8end)
0
Thanks man. I'll give it a try! Xyternal 247 — 3y
0
THANK YOU it worked like a dream Xyternal 247 — 3y
Ad

Answer this question