In Introduction to Roblox Studio, you learned how to create and manipulate parts in Roblox Studio. In this tutorial, you'll learn how to apply a script to parts to make a platform appear and disappear. You can use this in a platforming experience to span a gap, challenging users to time their jumps carefully to get to the other side.
Setting the Scene
First off, you need a Part to act as the platform. Making and moving parts should be familiar to you from Introduction to Roblox Studio. You don't need a complicated world aside from the platform — you just need a gap that your users can't easily jump across.
Insert a Part and rename it to DisappearingPlatform.
Resize it to large enough for a user to jump on.
Move it to a proper location so that you can reach it and jump on it when testing your experience.
Set the Anchored property to true in the Properties window.
Inserting a Script
Code in Roblox is written in a language called Lua, and it's stored and run from scripts. You can put scripts in various containers in the Explorer. If you put a script under a Part, Roblox will run the code in the script when the part is loaded into the game.
Hover over the DisappearingPlatform part in the Explorer window and click the + button to insert a new script into the platform. Rename your new script as Disappear.
Delete the default code inside.
It's a good idea to start off your script by making a variable for the platform. A variable is a name associated with a value. Once a variable is created, it can be used again and again. You can change the value as needed.
In Lua, a variable is created as follows: local variableName = variableValue.
The term local means that the variable is only going to be used in the block of the script where it's declared. The = sign is used to set the value of the variable. Names for variables are typically written in camel case. This is lowercase with every word following the first being capitalized, justLikeThis.
Copy the following code to create a variable for the platform called platform. where the value is script.Parent.
local platform = script.Parent
Time to make the platform disappear. It's always best to group code for achieving a specific action into a function. A function is a named block of code that helps you organize your code and use it in multiple places without writing it again. Create a function in the script and call it disappear.
local platform = script.Parentlocal function disappear()end
The first new line declares the function — it indicates the start of the function and names it as disappear. The code for a function goes between the first line and end.
The parentheses are for including additional information as needed. You'll learn more about passing information to functions in a later course.
When the platform disappears, it needs to be invisible and users need to fall through it — but you can't just destroy the platform since it needs to reappear later.
Parts have various properties that can be used here. Remember that you can see a part's properties if you select it and look at the Properties window.
A part can be made invisible by changing the Transparency property. Transparency can be a value between 0 and 1, where 1 is fully transparent and therefore invisible.
The CanCollide property determines if other parts (and users) can pass right through the part. If you set it to false, users will fall through the platform.
Just like script.Parent, properties are accessed using a dot. Values are assigned using an equals sign.
In the disappear function, set the CanCollide property of the platform to false.
On the line following, set the Transparency property to 1.local platform = script.Parentlocal function disappear()platform.CanCollide = falseplatform.Transparency = 1end
Calling the Function
Once you've declared a function, you can run it by writing its name with parentheses next to it. For example, disappear() will run the disappear function. This is known as calling a function.
Call the disappear function at the end of the script.local platform = script.Parentlocal function disappear()platform.CanCollide = falseplatform.Transparency = 1enddisappear()
Test the code by pressing the Play button. If your code works, the platform should have disappeared by the time the user spawns into the game.
You can easily make the platform reappear by writing a function which does the exact opposite of the disappear function.
Delete the disappear() line from the script.
Declare a new function called appear.
In the function body, set the CanCollide property to true and the Transparency property to 0.local platform = script.Parentlocal function disappear()platform.CanCollide = falseplatform.Transparency = 1endlocal function appear()platform.CanCollide = trueplatform.Transparency = 0end
The platform should be constantly disappearing and reappearing, with a few seconds between each change. It's impossible to write an infinite number of function calls — fortunately, with a while loop, you don't have to.
A while loop runs the code inside it for as long as the statement after while remains true. This particular loop needs to run forever, so the statement should just be true. Create a while true loop at the end of your script.
local platform = script.Parentlocal function disappear()platform.CanCollide = falseplatform.Transparency = 1endlocal function appear()platform.CanCollide = trueplatform.Transparency = 0endwhile true doend
Toggling the Platform
In the while loop, you need to write code to wait a few seconds between the platform disappearing and reappearing.
The built-in function wait can be used for this. In the parentheses the number of seconds to wait is needed: for example wait(3).
Three seconds is a sensible starting point for the length of time between each platform state.
- In the while loop, call the wait function with 3 in the parentheses.
- Call the disappear function.
- Call the wait function again with 3 in the parentheses.
- Call the appear function.
while true dowait(3)disappear()wait(3)appear()end
The code for the platform is now complete! Test your code now and you should find that the platform disappears after three seconds and reappears three seconds later in a loop.
You could duplicate this platform to cover a wider gap, but you need to change the wait times in each script, otherwise the platforms will all disappear at the same time and users will never be able to cross.
local platform = script.Parentlocal function disappear()platform.CanCollide = falseplatform.Transparency = 1endlocal function appear()platform.CanCollide = trueplatform.Transparency = 0endwhile true dowait(3)disappear()wait(3)appear()end