--- tags: Onboarding, Basic Coding --- # Deadly Arena So far, in [Introduction to Coding] and [Simple Trap], you’ve looked at changing basic properties by making some platform traps. Now you’ll be taking it a step further with a new, deadly kind of trap. You’ll be making a simple game, where players have to compete to survive on platforms which fall down into lava when touched. When players touch the lava, they’ll die and lose the game. <iframe src="https://player.vimeo.com/video/467702541" width="640" height="363" frameborder="0" allow="autoplay; fullscreen" allowfullscreen></iframe> ## Setting the Scene You’ll need a big space with a floor at the bottom that players will fall onto when the platforms drop. It’s up to you as to how to make your floor look deadly, but if you’d like to make the floor look more like lava, try setting the floor material to **Neon**, and the color to orange. ![](https://i.imgur.com/r5hDumo.png =400x) Players should be able to jump down onto the board of platforms and start playing once they join the game, so you’ll need a **spawn point** above the platforms. 1. Insert a spawn point by selecting **Spawn** in the **Model** tab and move it into place above where you want to put the dropping platforms. 2. **Resize** it so that it’s big enough to hold multiple players at a time. ![](https://i.imgur.com/kDAj9KX.jpg =600x) ## Connecting to the Touched Event If you’ve followed the [Introduction to Coding] and [Simple Trap] courses, you’ve connected to the Touched event before in your code. You’ll follow the same pattern here. 1. Insert a **Part** to use as the first platform. You’ll duplicate this later once you’ve written the code for it. ![](https://i.imgur.com/C1KI7iO.jpg =600x) 2. Insert a **Script** into the Part, **rename** it to ‘**Drop**’, then **delete** the default code. 3. Create a **variable** for the platform and an empty function connected to the platform’s **Touched** event. <pre class="code-line-highlights" data-start="1" data-highlight="1-7"> local platform = script.Parent local function drop() end platform.Touched:Connect(drop) </pre> Before you write any code for the behaviour of the platform, you’ll need to **debounce** the function using an if statement and a variable, the same way it was done in [Simple Trap]. 1. Create a variable to debounce the function called `isTouched` and assign it to **false** before the function. 2. Create an **if** statement with the condition `not isTouched` in the function. 3. Assign the variable to **true** inside the if statement. <pre class="code-line-highlights" data-start="1" data-highlight="3;6-8"> local platform = script.Parent local isTouched = false local function drop() if not isTouched then isTouched = true end end platform.Touched:Connect(drop) </pre> There’s no need to assign it back to false at the end - we want this to drop into the lava, so it should stay locked once the player touches it. ## Dropping the Platform You can drop the platform into the lava by setting its <a class="api-link" data-name="BasePart.Anchored">Anchored</a> property to **false** - but the platform shouldn’t just drop with no warning, or your players wouldn’t know to jump off it. The color of the platform can be used to warn the player that it’s about to drop. Part color is determined by the <a class="api-link" data-name="BasePart.Color">Color</a> property. This is stored as a <a class="api-link">Color3</a> object, which is a type specifically designed to store color. <div class="panel panel-info"> <div class="panel-heading"> Color3 </div> <div class="panel-body"> A Color3 object has three values to encode red, green, and blue (**RGB**). You can make any color by mixing these three. Each value is between 0 and 1, just like Transparency, with 1 being the maximum. (1, 0, 0) would be plain red, as it has the maximum amount of red and the minimum amount of green and blue. To make a Color3 object for red, you write `Color3.new(1, 0, 0)`. </div> </div> 1. Set the platform **Color** property to **red**. 2. Wait a second using the **wait** function. 3. Set the **Anchored** property to **false**. <pre class="code-line-highlights" data-start="1" data-highlight="8-10"> local platform = script.Parent local isTouched = false local function drop() if not isTouched then isTouched = true platform.Color = Color3.new(1, 0, 0) wait(1) platform.Anchored = false end end platform.Touched:Connect(drop) </pre> ## Destroy the Platform If you test your code now, you’ll find an issue - the platform drops into the lava, but it doesn’t go anywhere. If a player is standing on it when it falls, they’ll be able to just stand on the platform indefinitely, avoiding a fiery death. You’ll need to make it go away a few seconds after it drops to the ground. Just like how objects in Roblox have properties, objects can also have functions which you can call on them. Every object in Roblox has a <a class="api-link" data-name="Instance.Destroy">Destroy</a> function which - as you’d expect - destroys it. Note that this function is accessed with a **:**, not a dot. 1. **Wait** a few seconds after you unanchor the platform. 2. Call the **Destroy** function on the platform. <pre class="code-line-highlights" data-start="1" data-highlight="11-12"> local platform = script.Parent local isTouched = false local function drop() if not isTouched then isTouched = true platform.Color = Color3.new(1, 0, 0) wait(1) platform.Anchored = false wait(3) platform:Destroy() end end platform.Touched:Connect(drop) </pre> And that’s the platform completed! Test it out, and you should see that it lights up red, drops to the lava, then disappears. <iframe src="https://player.vimeo.com/video/467723745" width="640" height="363" frameborder="0" allow="autoplay; fullscreen" allowfullscreen></iframe> ## Scripting the Lava The lava doesn’t do anything yet - it needs to kill the player when they touch it. You’ll be starting your code to do this here the same way you started the platform code earlier. 1. Insert a **Script** into your floor Part. 2. Make a **variable** for the floor at the top of the script. 3. Create a **function** and connect it to the floor’s **Touched** event. <pre class="code-line-highlights" data-start="1" data-highlight="3-7"> local lava = script.Parent local function killPlayer() end lava.Touched:Connect(killPlayer) </pre> You’ll need to kill the player in the function - but how do you get the player? A Part’s Touched event can actually provide the other Part that touched it - but only if you ask for it, by making it a **parameter** of your function. <div class="panel panel-info"> <div class="panel-heading"> Parameters </div> <div class="panel-body"> Parameters are definitions of what a function expects to receive when it’s called. You can use a parameter in your function code the same way you would use any other variable. When you call a function, you fill the parameters by including values between the parentheses. For instance, in the code above, `killPlayer` is being passed to the **Connect** function in the parentheses. If you were able to see the definition of the Connect function, you’d find it has a parameter for the function it should run when the event fires. This parameter is filled in this case by the `killPlayer` function. </div> </div> When the Touched event runs killPlayer, it actually passes the **other Part** that touched the Part to the function. Because we haven’t had to use that other Part for anything in previous courses, the functions you’ve created to connect to Touched events in the past have not had a parameter for that to slot into. Now however, you do need to use it - so you need that parameter. Parameters are defined in the parentheses on the first line of a function. - Create a parameter called otherPart for the killPlayer function. <pre class="code-line-highlights" data-start="1" data-highlight="3"> local lava = script.Parent local function killPlayer(otherPart) end lava.Touched:Connect(killPlayer) </pre> When the `killPlayer` function is called, the `otherPart` parameter will now be filled with 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 player touches a Part, Roblox actually detects the specific bit of the player that touched it - for instance, the left leg, or the head. This Part is in the **Character** object for that player. The Character is the collection of all the physical Parts that make up the player’s avatar in the game. To get the Character in this case, you need to get the **parent** of the Part that touched the platform, as the parent of the player’s foot would be the Character. - Create a **variable** to store the **parent** of the Part that touched the lava floor in the `killPlayer` function. <pre class="code-line-highlights" data-start="1" data-highlight="4"> local lava = script.Parent local function killPlayer(otherPart) local partParent = otherPart.Parent end lava.Touched:Connect(killPlayer) </pre> You can’t just start treating this `partParent` variable as if it’s a Character though - you don’t yet know for sure that it’s actually a Character at all. A dropping platform could have touched the lava, for instance. <div class="panel panel-info"> <div class="panel-heading"> Humanoid </div> <div class="panel-body"> If the ` is indeed a Character, then it will contain a **Humanoid**. This is a special object which contains many properties relating to the player, including the player’s health. If you set the **Health** property of a Humanoid to 0, the player associated with that Humanoid will die. </div> </div> Unfortunately, you can’t set the Health property to 0 straight away. What if `partParent` doesn’t have a Humanoid in it? Trying to change the Health property of a non-existent object would cause an **error**, which would mean your code would no longer run. You need to find out if `partParent` has a Humanoid in it before you change the Health property to kill the player. We can get the Humanoid with the <a class="api-link" data-name="Instance.FindFirstChild">FindFirstChild</a> function. All objects in Roblox have this function: you pass it the name of the thing you’re looking for, and it will **return** the first thing it finds in that object which has that name. Functions can return values to you when you run them. You’ll learn how to make a function do that in the next course, but for now, you just need to know that you can store the value a function returns in a variable. - Call the **FindFirstChild** function on the `partParent` variable with “**Humanoid**” as an argument, and store the result in a variable called `humanoid`. <pre class="code-line-highlights" data-start="1" data-highlight="5"> local lava = script.Parent local function killPlayer(otherPart) local partParent = otherPart.Parent local humanoid = partParent:FindFirstChild("Humanoid") end lava.Touched:Connect(killPlayer) </pre> If FindFirstChild can’t find anything, it will return **nil** - this is a representation of ‘nothing’ in Lua. So if `partParent` isn’t a Character, `humanoid` will be empty. ## Checking the Humanoid Time to check if the Humanoid was actually found before moving onto setting the Health property. You can do this easily using an **if** statement, only running code to set the Health property if the `humanoid` variable isn’t nil. <div class="panel panel-info"> <div class="panel-heading"> ‘Truthy’ values </div> <div class="panel-body"> Values that aren’t strictly true or false can still be used in a conditional statement. Anything with any value other than **false** or **nil** is considered to be equivalent to **true** in Lua. This means you can be nice and efficient here by just sticking the humanoid variable directly into the condition of an if statement. </div> </div> 1. Create an **if** statement with `humanoid` as the condition. 2. In the body of the if statement, set the **Health** property of humanoid to **0**. <pre class="code-line-highlights" data-start="1" data-highlight="6-8"> local lava = script.Parent local function killPlayer(otherPart) local partParent = otherPart.Parent local humanoid = partParent:FindFirstChild("Humanoid") if humanoid then humanoid.Health = 0 end end lava.Touched:Connect(killPlayer) </pre> With that, your lava floor is complete! Test your game now, and you should find that your deadly lava successfully kills players on contact. You can now duplicate your scripted platform many times to create a simple game, where players try to outlast each other on a board of dropping platforms. Give it a go with a friend and see who can survive the longest. [footage of players playing the finished game]