String Variables

5 min

String Variables

Learning Objectives Students will be able to:
  • Write a variable to store a line of text.
  • Test code by displaying a string variable in the Output Window.
Prerequisites Students should:

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.

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 many variable types. String variables hold a mix of letters and numbers. They’re used to store information like names. String type variables always sit inside of quotation marks, "like this".

Examples of strings:

  • "You just joined the game!"
  • "There are 50 players left"
  • "10"

Code Your Own Message

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

  1. In the Explorer, hover over ServerScriptService. Click the
    and select Script.
  1. In the Script Editor, replace "Hello World" with a string that includes your favorite animal.

Testing Output

To test your code, use the Output Window. This window shows print output and errors from scripts.

To open the Output Window and test your script:

  1. Select the View menu tab.
  2. Click Output. The window will appear at the bottom of Roblox Studio.
  1. To test the script, press Play. Your new string will show up in the Output window.
  1. Since you’ve tested the script, Stop the playtest. You can now return to the Script tab to make any more changes or test out other strings.

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

Troubleshooting Your Code

No matter how long you’ve been coding, sometimes things won’t run on the first try. 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, 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. Notice the red error in the Output Window. Each error has a unique way of describing it. In this case, a “malformed string” means that string is missing something.
  1. Click on the red message in the Output window; this will take you to where the error is found in your code.