Variables

A variable is a name that holds a value. Variable values can be numbers, strings, booleans, data types, and more.

Naming Variables

Variable names can be any non-reserved string of letters, digits, and underscores that doesn't start with a digit.


1LETTERS -- valid
2a1 -- valid
3var_name -- valid
4_test -- valid
5
6if -- NOT valid
725th -- NOT valid
8

Variable names are case-sensitive, so TestVar and TESTVAR are different names. Avoid naming variables with an underscore and all uppercase letters, such as _VERSION, because Luau may reserve them for internal global variables.

Best Practices

It's best practice to spell out words fully. Abbreviations generally make code easier to write, but harder to read. Following common naming practices when naming your variables can help you and others understand their meaning or purpose:

  • Use PascalCase names for class and enum-like objects.
  • Use PascalCase names for all Roblox APIs. camelCase APIs are mostly deprecated.
  • Use camelCase names for local variables, member values, and functions.
  • Use LOUD_SNAKE_CASE names for local constants (variables with values that you don't expect to change).
  • Don't capitalize entire acronyms within names. For example, write aJsonVariable or MakeHttpCall.

Reserved Names

Luau reserves the following keywords, so you can't use them to name variables or functions:

  • and
  • for
  • or
  • break
  • function
  • repeat
  • do
  • if
  • return
  • else
  • in
  • then
  • elseif
  • local
  • true
  • end
  • nil
  • until
  • false
  • not
  • while

Assigning Values

To create a variable and assign a value to it, use the = operator. Put the variable on the left of the = and the value on the right. If you don't put a value, the value is nil.

Variables can have global or local scopes. They have global scope by default, but it's almost always better to create them with local scope because Luau accesses local variables faster than global ones. To give a variable local scope, put the keyword local before a variable's name when you assign a value to it. For more information on Scope in Luau, see Scope.


1local nilVar
2local x = 10
3local word = "Hello"
4local reference = workspace.Camera
5
6print(nilVar) -- nil
7print(x) -- 10
8print(word) -- Hello
9print(reference) -- Camera
10

Assigning Values to Multiple Variables

You can assign values to multiple variables in one line by separating each variable-value pair with a comma. If you have more values than variables, Luau doesn't assign the extra values to any variables. If you have more variables than values, then Luau assigns nil to the extra variables.


1local a, b, c = 1, 2, 3
2local d, e, f = 4, 5
3local g, h = 7, 8, 9
4
5print(a, b, c) -- 1, 2, 3
6print(d, e, f) -- 4, 5, nil
7print(g, h) -- 7, 8
8

Changing Values

To change a value of a variable, assign another value to it.


1local x, y = 10, 20
2print(x) -- 10
3print(y) -- 20
4
5local x = 1000
6local y = 2000
7print(x) -- 1000
8print(y) -- 2000
9