Coding is the process of creating instructions for computers to follow. Just like people use different languages such as English and Spanish, so do programs. Roblox uses the coding language Lua.
This article will cover how to start coding in Roblox, introducing common concepts like scripts and variables. By the end, you'll be able to type out code that displays messages in Roblox Studio
Coding with Scripts
In Roblox Studio , lines of Lua code are held in scripts. These scripts give the game sets of instructions on how to give players health points, create a rain of fireballs, or anything else imaginable.
Creating a Script
Scripts are commonly created in ServerScriptService, a special folder for holding and running scripts.
In the Explorer, hover over ServerScriptService to see the + button.
Click the + button and select Script. This opens the script editor.
Right-click on the Script and select Rename. Type in PracticeScript. Naming scripts is an important way of remember what each one does.
Using the Script Editor
Whenever you create new scripts, the script editor will automatically open up. This is where you will type your code in.
To find the script next time you open up Roblox Studio, click on the name of the script above the game editor, or double-click the script's name in the Explorer.
Whenever you create a new script, at the top of the script editor, is the print function. Print functions display text to the screen. It's one of the first functions many people learn since it's a simple way of giving the script a command.
Below is an example of the print function typed out and it's output, what you'll see once the script runs.
print("Hello world!")
Expected Output
Hello world!
String Variables
Remember back when you created a script. At the top of it was a message saying "Hello World". That message in quotations is an example of a string variable type. Variables are containers for information the program can use and change, like player names or points.
Lua variables can hold a variety of data types. Each data type holds a different kind of information, like a number, letter, or even a color. String variables, like the "Hello World", are a common data type that hold a mix of letters and numbers. String type variables always sit inside of quotation marks, "like this".
Examples of Strings
Some examples of string variables are below. Notice how they hold a mix of letters and numbers.
- "You just joined the game!"
- "There are 50 players left"
- "10"
To practice using string variables, you'll code a message in a script.
Typing a Variable
In Lua, while there are different types of variables, you'll start with local variables, which are found in most scripts. To type variables, start with typing local, followed by a name that describes what that variable should contain.
For example: local myName
Now, try coding it in a script. You'll write a message to store your favorite animal.
In the Explorer, hover over ServerScriptService. Click the plus sign and select Script. Rename the script to something like VariableTest.
In the script, remove the Hello World message. Start a variable by typing local, then write the variable name, myAnimal.
local myAnimal
Setting Variables
When a variable is just created, it doesn't have anything inside. To set it, or put something inside its container, use the = symbol. In this case, you'll set the variable you created to a string data type.
After the variable name, type =.
local myAnimal =Still on the same line, type a string variable that describes your favorite animal. Make sure all words and letters are between quotation marks.
local myAnimal = "Porcupines are my favorite animal.."
Using Print()
Print functions display text to the screen. It's one of the first functions many people learn since it's a simple way of giving the script a command. To see your variable, use the print() function.
On a new line, type print().
local myAnimal = "Porcupines are my favorite animal.."print()To print something, it must be inside the parentheses. To print the variable, type it inside, such as (myAnimal).
local myAnimal = "Porcupines are my favorite animal.."print(myAnimal)
Testing Output
To test your code, use the Output Window. This window shows print output and errors from scripts.
Select the View menu tab.
Click Output. The window will appear at the bottom of Roblox Studio.
To test the script, press Play. Your new string will show up in the Output window.
Since you've tested the script, Stop the play- test. You can now return to the Script tab to make any more changes or test out other strings.
Troubleshooting Tips
If the message didn't appear, try the following tips.
- Make sure print is lowercase.
- The string is surrounded by quotation marks: "like this".
- The variable is inside the parentheses like (myVariable).