text: Lua/Variables
  Lua modules based on the Scribunto/Lua extension are stored in resource pages using the Module: namespace. Each module uses a table to hold functions and variables, and that containing table is returned at the end of the module code. Variables are places that store values[1] This lesson will show you how to add variables to your functions.
  Prerequisites[edit]
  This lesson assumes you have already completed the Modules lesson.
  Create a Lua Script with a Variable[edit]
  To create a Lua script with a variable:
  -.variables() local variable = 1 variable = variable + 1 return variable end return p
  Test Your Lua Script[edit]
  To test your Lua script:
  - Navigate to either the Module_talk:Sandbox page, the Wikiversity:Sandbox page, or your own user or sandbox page.
  - Add the following code and save the page:
  {{#invoke:Sandbox|variables}}
  The result should be:
  2
  Understand Your Lua Script[edit]
  To understand your Lua script:
  localdefines the scope of the variable being declared.
  - If you are familiar with variable scope, be aware that Lua variables are global by default. Best practice for programmers is to use local variables whenever possible, so variables should be declared with the
  localkeyword in most cases. Using local in this function ensures that the variable's value can only be accessed and modified inside the function.
  variabledefines the name of the variable.
  - Variable names are flexible, but cannot be a Lua keyword. For example, you could not have a variable named
  local.
  = 1assigns the value
  1to
  variable.
  - Variables that are not given an initial value are equal to
  nil, indicating that they have no value.
  variable = variable + 1takes the value of
  variableand adds
  1to it, then stores the result of the calculation back in
  variable.
  - Variables can be used as both the source and destination for expressions (calculations).
  return variablereturns the current value of variable as the result of the function.
  - Numeric values are implicitly converted to strings when they are returned from the module.
  {{#invoke:Sandbox|variables}}calls the Sandbox module
  variablesfunction.
  Each Lua function can have one or more variables used to contain values for the function.
  Conclusion[edit]
  Congratulations! You've now created, tested, and understood a Lua script with a variable. Continue on to the Expressions lesson.