Fading Trap

In Deadly Lava, you learned how to trigger code based on user behavior. This tutorial shows you how to make a platform which fades away when a user steps on it.

Setting Up

If you followed Deadly Lava, you can place your fading platform above the lava floor that users can't jump across.

  1. Insert a part and move it into place in your world. Name it FadingPlatform.

  2. Resize it so a user can jump on it.

  3. Make sure it's Anchored.

  4. Insert a Script into the part, rename it to FadeOnTouch, and remove the default code.

  5. Create a variable for the platform and an empty function connected to the platform's Touched event.


    1local platform = script.Parent
    2
    3local function fade()
    4
    5end
    6
    7platform.Touched:Connect(fade)
    8

Fading the Platform

Having the platform vanish in an instant would be no fun at all as users would find it impossible to get across the gap. Instead, the platform should fade away before users fall through it to give them a chance to jump off.

You could change the Transparency property and wait a very short time over and over again to get this effect, but a gradual fade 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. Each loop of the code is known as an iteration. A for loop is defined with three things, separated by commas:

  • Control variable - The variable created and used to count the loops. In this example, it's count and the starting value is 1.
  • End value - The value it has to get to for the loop to stop. In this example, it's 10.
  • Step increment (optional) - Determines what to add to the control variable each loop. If left out, it defaults to 1, so in this example it's unnecessary.

In FadeOnTouch:

  1. In the function, create a for loop starting from 1 which iterates 10 times.

  2. Inside the for loop, set the Transparency property to the control variable divided by 10.

  3. Call the wait function with a time of 0.1.


    1local platform = script.Parent
    2
    3local function fade()
    4 for count = 1, 10 do
    5 platform.Transparency = count / 10
    6 wait(0.1)
    7 end
    8end
    9
    10platform.Touched:Connect(fade)
    11

When the loop runs, count increases by 1 with each iteration. This means that the platform's Transparency will increase by 0.1 every 0.1 seconds, reaching full transparency after 1 second.

Reappearing

After the platform has vanished, users should fall through it. The platform should also come back a few seconds after it fades - otherwise, users would never get to try the jump again if they failed. The CanCollide property controls whether users 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 property back to true.

  4. Set the Transparency property back to 0.


    1local platform = script.Parent
    2
    3local function fade()
    4 for count = 1, 10 do
    5 platform.Transparency = count / 10
    6 wait(0.1)
    7 end
    8 platform.CanCollide = false
    9 wait(3)
    10 platform.CanCollide = true
    11 platform.Transparency = 0
    12end
    13
    14platform.Touched:Connect(fade)
    15

Debounce Variable

In Deadly Lava, you learned that the Touched event runs each time a user's body part comes into contact with the part. This behavior causes issues when a user runs across the fading platform: the function will run multiple times, resetting the loop each time.

For the code to work properly, the function should only run once when the user touches the platform for the first time. Ensuring that an action is only triggered once when it would otherwise be triggered multiple times is known as debouncing.

To debounce a function, a boolean variable can be used to keep track of when the platform has already been touched. Booleans can only contain true and false values. Create a variable called isTouched and set it to false.


1local platform = script.Parent
2
3local isTouched = false
4
5local function fade()
6 for count = 1, 10 do
7 platform.Transparency = count / 10
8 wait(0.1)
9 end
10 platform.CanCollide = false
11 wait(3)
12 platform.CanCollide = true
13 platform.Transparency = 0
14end
15
16platform.Touched:Connect(fade)
17

Checking the Variable

An if statement can be used to only run the code in the fade function if the isTouched debouncing variable is false. Wrap the body of the fade function in an if statement with the condition not isTouched.


1local platform = script.Parent
2
3local isTouched = false
4
5local function fade()
6 if not isTouched then
7 for count = 1, 10 do
8 platform.Transparency = count / 10
9 wait(0.1)
10 end
11 platform.CanCollide = false
12 wait(3)
13 platform.CanCollide = true
14 platform.Transparency = 0
15 end
16end
17
18platform.Touched:Connect(fade)
19

The Lua not operator reverses the value of whatever follows it. In conditional terms, this means that the first if statement behaves the same as the statements which follow.


1if not isTouched then
2
3end
4if isTouched == false then
5
6end
7
8if isTouched == nil then
9
10end
11

Toggling the Debounce

Currently, the code in the fade function will always run because isTouched is false and not isTouched evaluates to true. To complete the debounce routine, you'll need to toggle the variable's value in two places.

  1. Set isTouched to true inside the if statement, before the platform begins to fade.

  2. Set it back to false once the platform has reappeared.


    1local function fade()
    2 if not isTouched then
    3 isTouched = true
    4 for count = 1, 10 do
    5 platform.Transparency = count / 10
    6 wait(0.1)
    7 end
    8 platform.CanCollide = false
    9 wait(3)
    10 platform.CanCollide = true
    11 platform.Transparency = 0
    12 isTouched = false
    13 end
    14end
    15
    16platform.Touched:Connect(fade)
    17

And that's it! Test your code now, and you should find that the platform fades away when the user jumps on it and comes back a few seconds later.

You can duplicate this platform across a wider gap to create a challenging obstacle and change the speed at which they fade to balance the difficulty.

Final Code


1local platform = script.Parent
2
3local isTouched = false
4
5local function fade()
6 if not isTouched then
7 isTouched = true
8 for count = 1, 10 do
9 platform.Transparency = count / 10
10 wait(0.1)
11 end
12 platform.CanCollide = false
13 wait(3)
14 platform.CanCollide = true
15 platform.Transparency = 0
16 isTouched = false
17 end
18end
19
20platform.Touched:Connect(fade)
21