Basic scripting tutorial: variables

Understanding Variables in Luau

Variables are esential in any programming language, and Luau, the language for Roblox scripting, is no exception. A variable acts as a container for storing information that your program can use later.


Declaring a Variable

To declare a variable in Luau, you use the local keyword followed by the variable’s name and an optional value:

local myVariable = 10

In this example, myVariable holds the value 10.


Rules for Naming Variables

  1. A variable name must start with a letter or an underscore (_).
  2. It can only include letters, numbers, and underscores.
  3. Reserved words like local, if, or function cannot be used as variable names.
  4. Variable names are case-sensitive, so myVariable and MyVariable are different.

Valid Examples

local playerScore
local _tempValue
local position2D

Invalid Examples

local 2ndValue -- Starts with a number
local if -- Reserved keyword

Types of Data in Variables

Variables can hold different types of data in Luau. Here are the main types:

  • Nil: Represents “nothing” or the absence of a value.

    local myVariable = nil
    
  • Number: Stores numeric values like 25, -10, or 3.14.

    local age = 25
    
  • String: Holds text enclosed in quotes.

    local playerName = "Alex"
    
  • Boolean: Stores true or false values.

    local isAlive = true
    
  • Table: A collection of data, like a list.

    local inventory = {"sword", "shield", "potion"}
    
  • Function: A block of code that can be executed (‘called’) later.

    local greet = function() print("Hello!") end -- To call greet, we do greet()
    
  • Instance: Refers to Roblox objects, like parts or players.

    local part = workspace.Part
    
  • You can find all types here, just click the ‘types’ tab on the left side:
    Luau | Documentation - Roblox Creator Hub


Dynamic Typing

Luau is dynamically typed, meaning a variable’s type can change during runtime. For example:

local myVariable = 42
myVariable = "Hello, World!" -- This is allowed

Variable Scope

In Luau, variables can be categorized based on their scope:

  • Global Variables: Declared without local. These can be accessed anywhere in the script but are generally discouraged as they may cause unexpected behavior.

    myVariable = 20
    
  • Local Variables: Declared with local. These are safer and only accessible within the part of the code where they are defined.

Scope Example

local function example()
    local localVar = 10 -- Only accessible inside this function
    globalVar = 20 -- Accessible anywhere in the script
end

example()
print(globalVar) -- Prints 20
print(localVar) -- Error: localVar is not defined here

Common Errors with Variables

  1. Using a Reserved Word: Reserved words cannot be used as variable names.

    local if = 10 -- Error
    
  2. Syntax Error: Occurs when you forget local or write something incorrectly.

    local 2value = 10 -- Error
    
  3. Type Mismatch: Happens when you use a variable in a way that doesn’t match its type.

    local has_score = true
    print(has_score+ 5) -- attempt to perform arithmetic (add) on boolean and number
    
3 Likes

You can add strings to a number, the string however must be a valid number

print(1 + "1")

image

2 Likes

My bad, you’re right. Should’ve been a bool.

2 Likes

You forgot to add some variables alternatives and variables types such as the following.


Declaring a Variable

To declare a variable in Luau, you use the local keyword followed by the variable’s name and an optional value:

local myVariable = 10

In this example, myVariable holds the value 10.

local firstVariable, secondVariable = true, "Hello!"

You can also declare multiple variables at once, in this example, firstVariable correspond to true and secondVariable correspond to "Hello!".


Types of Data in Variables

Variables can hold different types of data in Luau. Here are the main types:

  • Table: It can be considered a versatile container. Its primary use cases include storing a list of objects or values in the form of an array or a dictionary. Additionally, tables can be used to store data structures or to hold the functions and variables that make up an entire script.
local Array = {"Sword", "Shield", "Potion"}
local Dictionary = {
    Slot1 = Sword,
    ["Slot2"] = Shield,
    Slot3 = "Potion"
}
local Module = require(ReplicatedStorage.Module)
  • Function: A block of code that can be executed (‘called’) later.
local greet = function()
    print("Hello!")
end
local function greet()
    print("Hello!")
end

Dynamic Typing

Luau is dynamically typed, meaning a variable’s type can change during runtime. For example:

local myVariable = 42
myVariable = "Hello, World!"

In this example, 42 which is a number type were changed to "Hello, World!" which is a string type.

local model = workspace:FindFirstChild("model")
local part = model and model:FindFirstChild("part")

In this example, both model and part, can be nil or an instance depending of the result. The use of and is to verify if the model instance were found before trying to find the part instance.