Hello, I'am new to scripting and I want to make this block be able to kill a player when it touches it. Is there a way to do this?
How do I make a block that will kill players?
2 answers
You can either just find the humanoid or find the humanoid and :GetPlayerFromCharacter() . It all depends on what requirements you want the touched event to have. One basic way in writing this could be :
script.Parent.Touched:connect(function(hit) -- Assuming this script is in the block, server script. The "hit" is the parameter to connect the Touched event.
local hum = hit.Parent:FindFirstChild("Humanoid") -- You don't put hit.Parent.Character because the hit.Parent is the character. You also find the humanoid because you will eventually be taking health off of the player and this is where the health resides.
if hum ~= nil then --Scripters use this so we can check what is touching the brick is actually a humanoid, this way the script won't error or crash.
hum.Health = hum.Health - 100 --This will instantly kill the player, you can adjust the amount of health you want taken off by changing the "100" to whatever you want. To be on the safe side though you could do "hit.Parent:BreakJoints()". This is a safer way because a person with admin might change their health to lets say "10000000000" and the script would only subtract a 100, resulting in them not dying.
end
end)
If this helped you please don't forget to give a thumbs up and accept the answer. If it errored or you have any questions please comment on my answer and I will be glad to help.
10 comments
you could just put hum.Health = 0
Or you could also use TakeDamage(100) instead which is more prefered...
I said one of the basic ways lol
When spawning, a player character will get a force field so as to not get instantly killed by other players or environmental hazards. TakeDamage() will not damage the player if the character has a force field. This can be a problem for obstacle courses, where a player could try to rush through obstacles while he is invincible.
Also, a problem arises with humanoid.Health = humanoid.Health - 100 if the humanoid's MaxHealth property is above 100. The best is to directly set the Health property to 0, in this case.
@yougottols1 I'm sorry, but I have to downvote your answer. Your answer itself is relatively good, but your comments take more space than the code; it hurts readability. You could easily improve readability by shortening them or moving them outside of your code block. Do this, and I shall reconsider my downvote.
@Link150 I did say that :BreakJoints() Could work too to be more safer...
Harsh
or you can do on line 4,
hum.Health = 0
Try this:
script.Parent.Touched:connect(function(part)
if part.Parent:FindFirstChild("Humanoid") then
part.Parent.Humanoid.Health = 0
end
end)
This discussion is preserved as it was. New answers, comments, and votes are closed.

3 comments
http://wiki.roblox.com/index.php?title=Lesson:Lesson2
I'm not going to do the work for you. You have to learn to script on your own
Ok thanks