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.

local part = script.Parent

local function kill(otherpart)
    local partparent = otherpart.parent
    local humanoid = partparent:FindFirstChild("Humanoid")
    if humanoid then
        humanoid.Health = 0
    end
end


part.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 :)

local part = script.Parent

part.ClickDetector.MouseClick:Connect(function(plr) -- An event that activates a function when the part is clicked.
    local humanoid = plr.Character:FindFirstChild("Humanoid") -- Finds the players humanoid
    if humanoid then -- Checks if the player has a humanoid.
        humanoid.Health = 0 -- Kills the player
    end
end)

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