I'm really new in scripting.
I started my first game and it have a kill block, when player touch it, it should kill the player.
My code:
local trapPart = script.Parent local function onPartTouch(otherPart) local partParent = otherPart.Parent local humanoid = partParent:FindFirstChildWhichIsA("Humanoid") if humanoid then -- Set player's health to 0 humanoid.Health = 0 end end trapPart.Touched:Connect(onPartTouch)
I have no idea why this doesn't work, can you help me?
Lol, that script is outdated.
You should use this instead:
script.Parent.Touched:Connect(funtion(hit) if hit.Parent:FindFirstChild("Humanoid") then hit.Parent:BreakJoint() end end)
Anddd.. Boom, you are done. :)
I think your script it's outdated so it didn't work for you.
Scripts that kill players were easy to do. So, let figure out how it works.
script.Parent.Touched:Connect(function(hit) end)
Function 'hit' is a neutral function, so if the script's parent touched this will connect function 'hit'.
'end)' is a thing that automatically adds while your connect's bracket has a function.
script.Parent.Touched:Connect(function(hit) if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then end end)
Line 2 means if 'hit' and the parent (People) who touched and find a thing named 'Humanoid'.
'end' is used for ends the if...then statement.
script.Parent.Touched:Connect(function(hit) if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then hit.Parent.Humanoid.Health = 0 end end)
Line 3 shows the people who touched will get the humanoid and make the health become 0.
I hope I helped, and thanks for reading patiently! I hope you have a nice day :D, bye!