Learning some basic Roblox Studio code can make your games and projects have much more excitement. In this post we will show you how to write the script for a Fading Trap part that is perfect to put in an obby (obstacle course) or puzzle-style game! A fading trap is a block, or part, that slowly fades away until the player falls through. We will guide you step-by-step how to script a fading trap on Roblox in just a few steps.

For live guidance from an expert while learning to make cool games in Roblox, join our online classes designed by professionals from Google, Stanford, and MIT:

Learn How to Script a Fading Trap Part in Roblox

Since this is a great trap to put into an obby, we will be basing this tutorial on using an already placed part in an obb project. Be sure to keep a close eye on the line indentation as you work through these steps.

1. Open your Roblox obby game and choose a part

Start off by opening your favorite obby game creation in Roblox Studio and choose one part that you would like to have become a fading trap. Let’s click on this part, and in the Explorer window, rename the part: FadingPart (notice it is all one word).

Pick a part in your obby

2. Create a Roblox script for FadingPart

When you hover over the name FadingPart in the Explorer window, a plus sign appears. Click on this plus sign and in the menu that drops down, find and select the word Script. Once the script appears in the main window, go ahead and delete the default script, print(“Hello World!”). This is also a great time to rename your script to something that makes it easy to know the purpose of the script. In this case, let’s rename it: FadeOnTouch.

Rename part

3. Name your variable and create a function

To start your script, you will need to let the script know what part in the workspace to apply to, which in this case is a platform that is the “Parent” of this script. A parent is the next highest object above the script (therefore, the script is a “Child” of our FadingPart platform.

What the following code does is tell the game where the script is located in the workspace, that we are calling FadingPart “platform,” and then we are creating a new function (where the action steps are found) called “fade” for this platform. The last line (currently on line 7) is what calls the function to be used in the game - when “platform” is touched, start the “fade” function!

4. Add a loop to repeat a process

Next we will add a “for” loop inside our function, which will create the loop of the platform slowly becoming more and more transparent every time the game loops back over it. For loops are great for shortening your scripts, because they can take ten or twenty lines of repetitive code and condense them into three or four lines! In this case, we will create a loop that counts up by 1 until it reaches 10 times, then ends. Notice we add this loop INSIDE our function and it is indented one tab to the right.

Loop

It is very important to include the wait(0.1) line here, as without it, the game will run this loop so fast it could crash the game! This adds a slight pause between loops, of 1/10th of a second.

5. Bring back the platform again

So far, if you play this game with the script above, the platform will disappear, but it won’t come back so you can try again! That won’t be very fun if you fall through it and have to give it another go. We need to bring the platform back after it fully disappears. The next part of the script comes after the loop finally ends, and tells the game, “OK, the player can now fall through (canCollide = false). Then we wait for 3 seconds, bring the part back (no longer transparent), and they can touch it as a solid object again.

For Loop in Roblox

Notice that these four lines are indented at the same level as the For Loop above it.

6. Tell the game to “debounce”

Currently, our loop starts every time the platform is touched. That means every step a person takes will start the loop over without finishing it first. That creates a very “glitchy” feel and isn’t the smooth look we want to have. The next lines of code will tell the game that this script should only run once, the first time the player touches it. First, we add this line above the function:

Next, we add four more lines of code. The first two come before the For Loop, and tells the game to watch for when the player IS touching the platform (“is not false”), then says yes, it sure is. The second lines come after the remaining loop, and resets the touch to false, and then is a simple “end” statement to this condition (if), letting the game know that it needs to stop looking for the player touching the platform.

Notice again that the indentation changes for the rest of the lines between - be sure to adjust them accordingly!

7. Test it!

Now that you have your script in place, go ahead and press play to test the Fading Part. Hopefully, it looks something like this:

Script Your Own Fading Traps on Roblox

Now you can create a fading trap platform in Roblox Studio. If you enjoyed this script, but would like to see how to create this code in more detail, we have a YouTube video that walks through the same steps shown here, with much more detail on how the game reads each of the sections we scripted above.

For more fun with Roblox scripting, be sure to check out our post on How to Use Conditional Statements, or to learn more about loops, functions, and all kinds of scripts, try out our free Roblox class led live by an expert! Join along as an instructor guides you through building your own games with scripting using Roblox Studio in a small, live group session.

Written by Kari Tonkin, a Create & Learn instructor. Kari has been teaching a wide variety of courses and ages for more than twenty-six years. Some of her favorite subjects to teach include computer science, graphic design, mathematics, and coding languages. She received a Master’s Degree in Curriculum Design with Technology Integration from Black Hills State University in 2016 and has used this knowledge to stay current on new technology trends in education. When she is not working, Kari enjoys playing video games with her family, including Roblox and Minecraft, hiking and camping, and traveling around the United States.