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.
Insert a part and move it into place in your world. Name it FadingPlatform.
Resize it so a user can jump on it.
Make sure it's Anchored.
Insert a Script into the part, rename it to FadeOnTouch, and remove the default code.
Create a variable for the platform and an empty function connected to the platform's Touched event.
local platform = script.Parentlocal function fade()endplatform.Touched:Connect(fade)
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:
In the function, create a for loop starting from 1 which iterates 10 times.
Inside the for loop, set the Transparency property to the control variable divided by 10.
Call the task.wait() function with a time of 0.1.
local platform = script.Parentlocal function fade()for count = 1, 10 doplatform.Transparency = count / 10task.wait(0.1)endendplatform.Touched:Connect(fade)
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.
Set the CanCollide property of the platform to false after the for loop.
Wait for a few seconds using the task.wait() function.
Set the CanCollide property back to true.
Set the Transparency property back to 0.
local platform = script.Parentlocal function fade()for count = 1, 10 doplatform.Transparency = count / 10task.wait(0.1)endplatform.CanCollide = falsetask.wait(3)platform.CanCollide = trueplatform.Transparency = 0endplatform.Touched:Connect(fade)
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.
local platform = script.Parent
local isTouched = false
local function fade()
for count = 1, 10 do
platform.Transparency = count / 10
task.wait(0.1)
end
platform.CanCollide = false
task.wait(3)
platform.CanCollide = true
platform.Transparency = 0
end
platform.Touched:Connect(fade)
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.
local platform = script.Parent
local isTouched = false
local function fade()
if not isTouched then
for count = 1, 10 do
platform.Transparency = count / 10
task.wait(0.1)
end
platform.CanCollide = false
task.wait(3)
platform.CanCollide = true
platform.Transparency = 0
end
end
platform.Touched:Connect(fade)
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.
if not isTouched thenendif isTouched == false thenendif isTouched == nil thenend
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.
- Set isTouched to true inside the if statement, before the platform begins to fade.
- Set it back to false once the platform has reappeared.
local function fade()
if not isTouched then
isTouched = true
for count = 1, 10 do
platform.Transparency = count / 10
task.wait(0.1)
end
platform.CanCollide = false
task.wait(3)
platform.CanCollide = true
platform.Transparency = 0
isTouched = false
end
end
platform.Touched:Connect(fade)
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
local platform = script.Parent
local isTouched = false
local function fade()
if not isTouched then
isTouched = true
for count = 1, 10 do
platform.Transparency = count / 10
task.wait(0.1)
end
platform.CanCollide = false
task.wait(3)
platform.CanCollide = true
platform.Transparency = 0
isTouched = false
end
end
platform.Touched:Connect(fade)