Skip to content

Declaring Variables

Variables⚓︎

A variable is basically a name that can hold values. These values can be numbers, strings, tables and other Roblox data types.

Naming Variables⚓︎

Variable name can contain English letters, numerical digits and underscores. A variable name can start with letters and underscores but can not start with digits.

Correct Names

Eden
Teerach_Noob
Rose22
_Willie

lua is a case-sensitive language. This means, Eden and eden will be treated as two different variables. Keywords such as for, break, true, local, etc can't be utilised as variable names.

Wrong Names

if
21st_century

Assigning Values⚓︎

The operator = is used for assignment of values. Consider the following example

sloth = "a lazy animal"
print(sloth)
a lazy animal

A declared variable can be changed anytime by assigning another value to it.

Deden = 4
print(Deden)
Deden = 17
print(Deden)
4
17

Multiple assignment⚓︎

Lua also enables its users to declare multiple variables at once. It can be done by separating variable names and their values by ,.

Salzu, Paper, Eden = "Fox", "China", "Rose"
In the above example variables will be assigned values in serial order.

Scopes⚓︎

In lua, a variable can be declared in any of the two scopes local and global.

Global Variables⚓︎

Variables declared in global scope is accessible in all the scopes of a script. Every variable by default is a global variable unless declared with the keyword local.

do -- first scope
  x = 9
  print(x)
end 

do -- second scope
 print(x)
end 
9
9

The variable declared in first scope is accessible in second scope.

Warning

Because global variables and functions must be accessed by a hash lookup, they can be expensive to use in terms of performance. In fact, a global variable accessed in a time-critical loop can perform 10% slower (or worse) than a local variable in the same loop. As noted earlier, global variables and functions are only accessible within the associated script, not between multiple scripts.

Local Variables⚓︎

Local variables are declared with the keyword local and are only accessible in the scope in which they are defined.

do -- first scope
  local x = 9
  print(x)
end 

do -- second scope
  print(x)
end 
9
nil

The local variable declared in first scope is accessible only in first scope and is nil for second scope.

Closing!⚓︎

Thanks for reading! I hope you enjoyed. We aren't perfect. We make mistakes, typos, etc. so if you found a mistake while reading this article, or any article for that matter, feel free to report it, and perhaps you can review us too! Any feedback is appreciated.

Comments