In Introduction to Coding, you learned how to make changes in a game in a loop over time. What if you want to make changes based on player behavior?
This course will show you how to make a deadly lava floor which kills a player when they step on it.
You’ll need a place in your game world to put the deadly lava. If you followed the Introduction to Coding 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 game world — call it LavaFloor.
Resize it so it covers the floor of the enclosing space.
Make the floor look more like lava by setting the material to Neon and the color to orange.
Insert a Script into the part and rename it to KillPlayer.
Remove the default code and create a variable for the lava.
local lava = script.Parent
Connecting to an Event
You’ll need to use an event to detect when a player 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
Access the Touched event on the lava object using a dot, just like a property:
Connectfunction on the Touched event, passing the kill function.
local lava = script.Parent local function killPlayer() end lava.Touched:Connect(killPlayer)
Any code you write in the
killPlayer 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 player, the function will need an object associated with that player. 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
local lava = script.Parent local function killPlayer(otherPart) end lava.Touched:Connect(killPlayer)
killPlayer 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.
Next Page Killing the Player