In Introduction to Roblox Studio, you learned how to create and manipulate parts in Roblox Studio. It’s time to use code to make them do things.
In this project, you’ll use a script to make a platform appear and disappear. You could use this in a platforming game to span a gap, challenging players to time their jumps carefully to get to the other side.
Setting the Scene
First off, you’ll 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 game world aside from the platform — you just need a gap that your players can’t quite jump across.
Insert a Part and give it a name like DisappearingPlatform.
Resize it so that it’s big enough for a player to jump on.
Move it so that you can reach it and jump on it when testing the game.
Set the Anchored property to true in the Properties window.
Remember that setting a part’s Anchored property to true will make it stay in place no matter what. Your platform will fall down if it’s not anchored.
Inserting a Script
Code in Roblox is written in a language called Lua and is stored and run from scripts. You can put scripts anywhere — if you put a script in a part, Roblox will run the code in the script when the part is loaded into the game.
Hover over your Part in the Explorer and click the button.
Select Script from the menu which appears — this will insert a new script into the platform. Call your new script Disappear.
Delete the default code inside.
Naming Scripts and Parts
Remember to rename parts and scripts as soon as you create them so you don’t lose track of things in the Explorer.
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.
local just means that the variable is only going to be used in this part of the script. The
= sign is used to set the value of the variable.
- Copy the code below to create a variable for the platform called
platform. where the value is
local platform = script.Parent
script.Parent is used to find the object the script is located in. As you might have guessed,
script refers to the script you’re writing in and the
Parent of the script is where it’s located.
Names for variables are typically written in camel case. This is lowercase with every word following the first being capitalized,
Next Page Functions