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
TestVarTESTVAR_VERSIONThe following keywords are reserved by Lua and cannot be used as variables or function names:
| and | break | do | else | elseif | end | false |
|---|---|---|---|---|---|---|
| for | function | if | in | local | nil | not |
| or | repeat | return | then | true | until | while |
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,
partredgreenbluepurpleChecking 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