Assigning a value to a variable can be done using the = operator, like so:
x = 10 word = "Hello"
Variable x now has a value of the number 10, and word has a value of the string "Hello". If we print out these variables, we should see the value they have:
print(x) --> 10 print(word) --> Hello
A variable can be changed simply by assigning another value to it:
n = 12 print(n) --> 12 n = 24 print(n) --> 24
Even though n was assigned to a value of 12, its value was overwritten by the second assignment of 24.
You can also use a variable as a value when assigning.
n = 78 x = n print(x) --> 78
When x was assigned to n, it's value became the value of n.
Remember! A variable is always on the left side of the = while a value is on the right side.
Important! If you assign a variable to a property's value (e.g. Transparency), the variable will not change when the property does. To make a variable update with a property's value, you can include the code below in your scripts:
script.Parent.Changed:connect(function() var = script.Parent.Name -- Change the path and property name as appropriate to what you need end)
The name you give a variable can only contain letters (uppercase and lowercase), numbers, or an underscore ( _ ), this means that you shouldn't use space characters in the name of your variable. It must start with an underscore or a letter. It cannot be a keyword (one that is highlighted blue in the editor).
LETTERS = "ABC" -- valid x86 = 1978 -- valid var_name = "variable" -- valid _ = "blank" -- valid if = 12 -- NOT valid! 16th = "Lincoln" -- NOT valid!
Remember! You must make sure when you call the variable later on in the script that you use the same uppercase and lowercase letters as you did when assigning it, e.g. using the example code above, Letters ~= "ABC" but LETTERS == "ABC".
Lua allows you to assign more than one value to more than one variable.
a, b, c = 1, 2, 3 print(a, b, c) --> 1 2 3
Each variable and each value is separated by a comma. Values to the right of the = are assigned to their corresponding variables to the left. The first variable is assigned to the first value, the second variable is assigned to the second value, and so on. The above example is the same as this. You can see how it saves a lot of space:
a = 1 b = 2 c = 3 print(a, b, c) --> 1 2 3
Multiple assignment is useful because it allows you to easily swap the values of two variables.
x, y = 8, 36 print(x, y) --> 8 36 x, y = y, x print(x, y) --> 36 8
In this example, x is assigned to the original value of y, and y is assigned to the original value of x.
Sometimes you may want to limit the access of one variable to another. You can do this by allowing a variable to be visible only to others in the same area. These areas are called scopes. In normal Lua, there are two scopes, known as global and local.
Each block of code has its own scope. Since blocks can be nested, each scope may have an outer scope and an inner scope.
A global variable is a variable that is visible to all scopes of a script. (Not to be confused with adding variables to the "_G" or "shared" tables, which are shared amongst scripts.)
a = 1 if true then print(a) --> 1 if true then a = 2 print(a) --> 2 end print(a) --> 2 end print(a) --> 2
All variables will default to global, unless they are declared local (explained next).
A local variable is a variable that can only be accessed by other things in the same scope. A variable is made local by adding the keyword local before the variable. It is true that local variables are obtained faster than normal variables. This is because they are integrated into the current environment they were created in.
local word = "Hello"
A local variable is not visible to things in its outer scope.
if true then -- a new block is created local b = 2 end print(b) --> nil
However, they are visible to things in its inner scope.
local b = 2 if true then print(b) --> 2 end
If a variable has the same name as one in its outer scope, the outer scope's value is overwritten in the inner scope, but not the outer scope.
a = 1 if true then local a = 2 print(a) --> 2 end print(a) --> 1
In Roblox Lua, there is a third type of variable called a shared variable. Shared variables are variables that are visible to every script (that is, they are shared between them). Some examples of a shared variable include print, game, or Instance. A list of all shared variables can be found at the Function Dump.
Remember that there are some major differences between normal Lua and Roblox Lua.