In Introduction to Scripting, you learned how to make changes in an 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.
Setting Up
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.
local lava = script.Parent
Connecting to an Event
Use an event to detect when a user touches the lava. Every part has a Touched event that 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.
local lava = script.Parentlocal function kill()endlava.Touched:Connect(kill)
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.
local lava = script.Parent
local function kill(otherPart)
end
lava.Touched:Connect(kill)
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.
local lava = script.Parent
local function kill(otherPart)
local partParent = otherPart.Parent
end
lava.Touched:Connect(kill)
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 provides 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.
local lava = script.Parent
local function kill(otherPart)
local partParent = otherPart.Parent
local humanoid = partParent:FindFirstChild("Humanoid")
end
lava.Touched:Connect(kill)
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.
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)
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 dies. In the body of the if statement, set the Health property of humanoid to 0.
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)
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.
Final Code
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)