String Variables

String Variables

Prerequisites General knowledge of Studio covered in Intro to Studio
Lesson Time 5 minutes
Optional Handouts Coding with Lua
Learning Objectives Students will be able to:
  • Use string variables to store a message
  • Test code to see printed output in the Output Window

Whenever you create a new script, at the top of the script editor is the print function print("Hello world!") It’s famous for being one of the first lines of code people learn how to write.

Print functions output text to the screen. Any time your phone sends you a notification, you’re seeing a print function in action!

Variable Types - Strings

"Hello World" is a string variable type. Variables are containers for information the program can use and change, like player names or points.

There are lots of types of variables. String variables hold a mix of letters and numbers. They’re good for things like names or addresses, but not good for counting. String type variables always sit inside of quotation marks, “like this”.

Examples of strings:

  • I am 100 years old"
  • 42
  • wo0t!

Code Your Own Message

Instead of saying Hello World, create your own message by changing the string in the print function.

  1. In the Script Editor, replace "Hello World" with "Blank are my favorite animals!".

Testing Output

To test your code, you’ll need to use the Output Window.

To open or close the Output window:

  1. Select the View menu tab.
  2. Click Output.

Test Your Script

The Output window displays print functions and helpful error messages for game developers whenever the game is run.

  1. Click Play to test your script. Your new string will show up in the Output window.

Returning to the Script

Now that you know how to test your script, let’s go back into the script editor.

  1. Stop the playtest.
  2. Go back to the script by clicking on the script editor.

Script Tab Missing?

If you don't see the script editor, double-click the name of the script in Explorer to reopen the script.

Make sure that students always click stop after testing a script. New users often forget to stop their game after testing.

Troubleshooting Your Code

No matter how long you’ve been coding, sometimes you’ll find errors that keep your program from running. Troubleshooting is the process of looking through your code to find and fix problems.

If your code didn’t work:

  • Make sure print is lowercase.
  • The string is surrounded by quotation marks: "like this".
  • The string is inside the parentheses: ("like this").
  • Ask a friend for help or try re-doing your work from the start.

Create an Error

To better understand what errors look like, let’s purposely create an error.

  1. Open the script.
  2. Delete a quotation mark from the print statement.
  3. Hover over the red line to see an error message. In this case, you should see Unfinished string meaning the string is missing a quotation mark.

Read an Error

Error messages show up as red lines in both the Script Editor and Output Window. You can use these error messages to help pinpoint the problem.

  1. Press Play to run your code.
  2. Click on the red message in the Output window; this will take you to where the error is found in your code.

These documents are licensed by Roblox Corporation under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Roblox, Powering Imagination, and Robux are trademarks of Roblox Corporation, registered in the United States and other countries.