Module 1-1: Variables

Introduction

Variables are super important. A program can't do anything if you only hard code data. Let's talk about them today.

Outline:

  • What is a Variable?
    • Variable Naming
  • Examples
    • Changing the BrickColor of a Part
    • Checking the Current Time

What is a Variable?

A variable is a named container that can hold a value. A variable lets your program remember information and later on it can reference that information when making decisions. Variable values can be numbers, strings, booleans, data types, and more.

Variable Naming

In Lua, which is the primary programming language that Roblox uses, variables names can be anything as long as it:

  • doesn't start with a number
  • is not just a number
  • doesn't have spaces
  • is not a "reserved" word

Note: Variables are case-sensitive, meaning

TestVar
and
TESTVAR
are two unique names. Names beginning with an underscore followed by uppercase letters (such as
_VERSION
should be avoided), as they may be reserved by Lua already.

The following keywords are reserved by Lua and cannot be used as variables or function names:

and break do else elseif end false
forfunctionifinlocalnilnot
orrepeatreturnthentrueuntilwhile

Examples

The following are some examples on how variables can be used in Roblox Studio:

Changing the BrickColor of a Part

Add this script inside of a Part and the Part will change into four different colors infinitely.

In the script above,

part
is a variable that references the game object that will change color. The other variables hold certain colors and are named
red
,
green
,
blue
, and
purple
.

Checking the Current Time

This script can be placed anywhere within the Workspace and will run only once to check what the current time of day is.

In the script above,

currentTime
is the variable that holds the time of day when you first run your project. You can see the output by clicking on the Output button in the Script tab.