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

How to kill a player when I click a part?

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!

4 comments

0

Those of you who will reference me to peasfactory, note that I have made this code inspired by his tutorial.

0

You need a click detector for mouseclick. it would be part.ClickDetector.MouseClick:Connect(kill)

0

otherpart should be the player who clicked it?

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.

1 answer

✓ Accepted answer

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)

2 comments

0

Thanks man. I'll give it a try!

0

THANK YOU it worked like a dream

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