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.
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.
Insert a Part and move it into place in your world. Name it LavaFloor.
Resize it so it covers the floor of the enclosing space.
Make the floor look more like lava by setting the Material property to Neon and the Color to an orange shade.
Insert a Script into the LavaFloor part and rename it to Kill.
Remove the default code and create a variable for the lava.1local lava = script.Parent2
Connecting to an Event
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.
Declare a new function called kill.
Access the Touched event on the lava object using a dot, just like a property: lava.Touched.
Call the Connect function on the Touched event, passing the kill function.1local lava = script.Parent23local function kill()45end67lava.Touched:Connect(kill)8
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.
Getting the Touching Part
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.
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.
1local lava = script.Parent23local function kill(otherPart)45end67lava.Touched:Connect(kill)8
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.
Character and Humanoid
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:
- The individual body parts of the user such as the head, limbs, and torso.
- Any clothing and accessories worn by the user.
- The Humanoid, a special object which contains many properties related to the user, including the user's health.
- The HumanoidRootPart which controls the user's movement.
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.
1local lava = script.Parent23local function kill(otherPart)4 local partParent = otherPart.Parent5end67lava.Touched:Connect(kill)8
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.
1local lava = script.Parent23local function kill(otherPart)4 local partParent = otherPart.Parent5 local humanoid = partParent:FindFirstChild("Humanoid")6end78lava.Touched:Connect(kill)9
Checking the Humanoid
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.
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.
1local lava = script.Parent23local function kill(otherPart)4 local partParent = otherPart.Parent5 local humanoid = partParent:FindFirstChild("Humanoid")6 if humanoid then78 end9end1011lava.Touched:Connect(kill)12
Setting Character Health
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.
1local function kill(otherPart)2 local partParent = otherPart.Parent3 local humanoid = partParent:FindFirstChild("Humanoid")4 if humanoid then5 humanoid.Health = 06 end7end89lava.Touched:Connect(kill)10
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.
1local lava = script.Parent23local function kill(otherPart)4 local partParent = otherPart.Parent5 local humanoid = partParent:FindFirstChild("Humanoid")6 if humanoid then7 humanoid.Health = 08 end9end1011lava.Touched:Connect(kill)12