| title | Fading trap |
|---|---|
| description | The process for creating a platform that fades away when a player steps on it. |
| next | /tutorials/use-case-tutorials/scripting/basic-scripting/score-points |
| prev | /tutorials/use-case-tutorials/scripting/basic-scripting/deadly-lava |
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.
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
Touchedevent.local platform = script.Parent local function fade() end platform.Touched:Connect(fade)
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
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 FadeOnTouch:
-
In the function, create a
forloop starting from1which iterates10times. -
Inside the for loop, set the
Transparencyproperty to the control variable divided by10. -
Call the
Library.task.wait()function with a time of0.1.local platform = script.Parent local function fade() for count = 1, 10 do platform.Transparency = count / 10 task.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, 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
CanCollideproperty of the platform tofalseafter the for loop. -
Wait for a few seconds using the
Library.task.wait()function. -
Set the
CanCollideproperty back totrue. -
Set the
Transparencyproperty back to0.local platform = script.Parent 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)
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)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 Luau 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 then
end
if isTouched == false then
end
if isTouched == nil then
endCurrently, 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
isTouchedtotrueinside theifstatement, before the platform begins to fade. - Set it back to
falseonce 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.
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)
