In Deadly Lava, you learned how to trigger code based on player behavior. This course will show you how to make a platform which fades away when a player steps on it.
If you followed the Deadly Lava course, you can place your fading platform above the lava floor — either way, it should go over a space that players can’t jump across.
Insert a part and move it into place in your game world - call it FadingPlatform.
Resize it so a player 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.Parent local function fade() end platform.Touched:Connect(fade)
Fading the Platform
Having the platform vanish in an instant would be no fun at all - players would find it 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 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
countand 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 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 wait function with a time of 0.1.
local platform = script.Parent local function fade() for count = 1, 10 do platform.Transparency = count / 10 wait(0.1) end end platform.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.
After the platform has vanished, players should fall through it. The platform should also come back a few seconds after it fades - otherwise, players would never get to try the jump again if they failed. The CanCollide property controls whether players 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 wait function.
Set the CanCollide property back to true.
Set the Transparency property back to 0.
local platform = script.Parent local function fade() for count = 1, 10 do platform.Transparency = count / 10 wait(0.1) end platform.CanCollide = false wait(3) platform.CanCollide = true platform.Transparency = 0 end platform.Touched:Connect(fade)
Next Page Debouncing