# Simple Trap In Introduction to Coding, you learned how to make changes to the game world in a loop over time. But what if you want to make changes based on behavior in your game? Instead of making a platform for an obby which disappears and reappears in a loop, this course will show you how to make a platform which only disappears when a player steps on it. {%vimeo 463075303 %} You’ll need to use **events** to detect when a player touches the platform. ## Setting Up You’ll need a place in your game world to put the disappearing platform. You could use the same world as you used if you followed the Introduction to Coding course, but all you really need is a space that players can’t jump across that needs to be filled with platforms. 1. **Insert** a Part and move it into place in your game world - call it something like **DisappearingPlatform**. 2. **Resize** it so a player can jump on it. 3. Make sure it’s <a class="api-link" data-name="BasePart.Anchored">Anchored</a>, and make sure it isn’t touching anything else. 4. Insert a **Script** into the Part, and rename it to **Disappear**. 5. Remove the default code and create a **variable** for the platform. <pre class="code-line-highlights" data-start="1" data-highlight="1"> local platform = script.Parent </pre> ![Placeholder](https://i.imgur.com/QBq0VxY.jpg =600x) ## Connecting to an Event Events are **members** of objects -- they can be accessed using a dot, the same way that properties are accessed. An event **fires** when the action they are detecting occurs in the game. In this case, you need to respond to players touching the platform, so you can make it disappear. Fortunately, every Part has a <a class="api-link" data-name="BasePart.Touched">Touched</a> event which fires when something touches it. You can **connect** to this event and can run code when it fires. Every event has a **Connect** function that can be used to do this. Connect has a function **parameter**, so the code you want to run should go into its own function which can then be passed to the Connect function. A parameter is a placeholder for additional information a function requires in order to run. You pass a value to a function’s parameter by providing it between the parentheses when it is called. 1. Declare a new function called `disappear`. 2. Call the **Connect** function on the **Touched** event of the platform, passing the `disappear` function. <pre class="code-line-highlights" data-start="1" data-highlight="3-7"> local platform = script.Parent function disappear() end platform.Touched:Connect(disappear) </pre> Any code in the `disappear` function will now run whenever something touches the platform. Note that a colon is used for the Connect function, not a dot - no need to worry about why just yet, but do make sure to remember it. ## For Loop Copying the code from Introduction to Coding and just having the platform vanish in an instant would be no fun at all - players would find it almost impossible to get across the gap. It would be better if the platform could **fade away** before players could fall through it, to give them a bit of warning and a chance to jump off it. You could change the <a class="api-link" data-name="BasePart.Transparency">Transparency</a> property and wait a very short time over and over again to get this effect, but to get a nice smooth fade, it would require at least 10 changes between 0 and 1. That’s 20 lines of very repetitive code. This can be achieved much more effectively using a **for** loop, which repeats code a specific number of times decided by a counter (a **control variable**) which you define. You can actually use the control variable in your own code as well. Each loop of the code is known as an **iteration**. 1. In the function, create a **for** loop with **10** iterations, starting from **1**. 2. Inside the for loop, set the **Transparency** property to the control variable divided by 10. 3. Call the **wait** function, passing **0.1** to it. <pre class="code-line-highlights" data-start="1" data-highlight="4-7"> local platform = script.Parent function disappear() for i = 1, 10 do platform.Transparency = i / 10 wait(0.1) end end platform.Touched:Connect(disappear) </pre> <div class="panel panel-info"> <div class="panel-heading"> For loop definition </div> <div class="panel-body"> A for loop is defined with three things, separated by commas: 1. **Control variable** - the variable created and used to count the loops. In this example, the starting value is 1. 2. **Limit** - The value it has to get to for the loop to stop. In this example, it's 10 3. **Step Increment** (optional) - Determines what to add to the control variable each loop. If left out, it defaults to 1. </div> </div> Inside the loop, **i** divided by 10 is used as the value to assign to <a class="api-link" data-name="BasePart.Transparency">Transparency</a>, followed by a wait for 0.1 seconds. i is going to change with every iteration, increasing by 1 each time. This means that Transparency is going to increase by 0.1 every 0.1 seconds, so it’ll fade away completely in 1 second when Transparency reaches 1. ## Reappearing After the platform has vanished, it should allow players to fall through it. The platform should also come back a few seconds after it disappears - otherwise, players would never get to try the jump again if they failed. You might recall from [Introduction to Coding] that the <a class="api-link" data-name="BasePart.CanCollide">CanCollide</a> property decides whether players can fall through a Part. 1. Set the **CanCollide** property of the platform to **false** after the for loop. 2. Wait for a few seconds using the **wait** function. 3. Set the **CanCollide** platform back to **true**. 4. Set the **Transparency** property back to **0**. <pre class="code-line-highlights" data-start="1" data-highlight="8-11"> local platform = script.Parent function disappear() for i = 1, 10 do platform.Transparency = i / 10 wait(0.1) end platform.CanCollide = false wait(3) platform.CanCollide = true platform.Transparency = 0 end platform.Touched:Connect(disappear) </pre> In theory, that should be everything required for the platform’s code - but if you try testing this, you’ll find it doesn’t work just yet. There’s one last step required before this code will work properly. <div class="panel panel-info"> <div class="panel-heading"> Touched Event Behaviour </div> <div class="panel-body"> If you were to put a print statement inside the disappear function - something like `print(“Hi!”)` - and test the game, you’d see the issue if you look at the Output window while standing on the platform. The Touched event **constantly** fires when the player is touching the platform. This means that the disappear function keeps running over and over again. When this happens, the assignments to Transparency and CanCollide in any given function call are overridden by the next function call, so the platform won’t fade away until you step off it and stop firing the event. </div> </div> ## Debouncing For the code to work properly, the function should only run a single time when the platform is touched. Ensuring that something is only triggered by an action once when it would otherwise be triggered multiple times is known as **debouncing**. The function needs to be **locked**, so that when it runs, it cannot run again until the platform reappears. To debounce a function, you first need a **Boolean** variable to keep track of when the platform has already been touched. Boolean just means the only values it can contain are **true** and **false**. In this case, the function is either already running, or it isn’t and can be run again. By default, the platform hasn’t been touched yet, so the variable starts off as false. You’ll set it to true later on. - Create a **Boolean** variable called `isTouched` and set it to **false**. <pre class="code-line-highlights" data-start="1" data-highlight="3"> local platform = script.Parent local isTouched = false function disappear() for i = 1, 10 do platform.Transparency = i / 10 wait(0.1) end platform.CanCollide = false wait(3) platform.CanCollide = true platform.Transparency = 0 end platform.Touched:Connect(disappear) </pre> ## If Statement As part of debouncing the function, you can use an **if statement** to only run the code in the disappear function when no one is touching it based on the value of the debouncing variable you created. The code in an if statement will only run if the condition defined in the first line is true. There are a [variety](https://www.lua.org/pil/3.2.html) of [operators](https://www.lua.org/pil/3.3.html) that can be used to build more complex conditions for if statements - you’ll encounter those in future courses. <div class="panel panel-info"> <div class="panel-heading"> not Operator </div> <div class="panel-body"> The condition needed here is ‘if isTouched is false’ - as in, if isTouched is false, then run the code in the function. In Lua, you could write if `isTouched == false`, where == means ‘is equal to’ - but there’s a slightly neater way of writing it. The **not** operator flips the truth value of whatever follows it, so you can instead write `if not isTouched` and get the same result. </div> </div> Just like with functions or for loops, the code in an if statement goes between the first line and the end. In this case, you need the whole function body to go in the if statement. - Wrap the body of the disappear function in an if statement, with the condition ‘not isTouched’. <pre class="code-line-highlights" data-start="1" data-highlight="6;15"> local platform = script.Parent local isTouched = false function disappear() if not isTouched then for i = 0, 1, 0.1 do platform.Transparency = i wait(0.1) end platform.CanCollide = false wait(3) platform.CanCollide = true platform.Transparency = 0 end end platform.Touched:Connect(disappear) </pre> ## Locking the Function Currently, the code in the `disappear` function is always going to run. The value of `isTouched` is never changed, so the condition in the if statement will always be true. 1. Set `isTouched` to **true** at the start of the code in the if statement 2. Set it back to **false** once the platform has reappeared. <pre class="code-line-highlights" data-start="1" data-highlight="7;16"> local platform = script.Parent local isTouched = false function disappear() if not isTouched then isTouched = true for i = 0, 1, 0.1 do platform.Transparency = i wait(0.1) end platform.CanCollide = false wait(3) platform.CanCollide = true platform.Transparency = 0 isTouched = false end end platform.Touched:Connect(disappear) </pre> And that’s it! You now have a platform which fades away when the player jumps on it, and comes back a few seconds later. You can duplicate this platform across a gap to create a good obstacle for a game - players will have to cross quickly to avoid falling. Try spacing and sizing them differently or changing the speed at which they disappear to balance the difficulty. {%vimeo 463452186 %}