Lua Gotchas

The programming language Lua is widely accepted as simple, elegant and easy to learn. It is also light-weight and very suitable for resource-intensive applications such as load testing. But, as with any other language, it has its own pitfalls. Here is a short list of gotchas that are most often encountered by people coming to Lua from other languages. (Note that in several code examples we use the function log.log() to print out text. This is the API function provided by the Load Impact API to log text messages during execution of a load script)

  • Conversion to boolean

Any value except `nil` and `false` is considered to be `true` in Lua. Note that this means that `0` is `true` as well:

 if 0 then"zero is true")
 else"zero is false")
 --> Prints "zero is true"
  • Variables are global by default

You have to explicitly declare local variables using keyword `local`. This means that a typo in variable name may potentially bring you trouble:

 local has_color = true
 if has_colour then -- Note typo"in color")
 else"in monochrome")
 --> Unexpectedly prints "in monochrome" since has_colour is nil

It is considered a good practice to declare all variables as local and to limit their scope as much as possible.

  • Arrays / tables

Lua has single all-powerful data structuring type, called “table”. All common data stuructures may be represented as a Lua table. Linear arrays in Lua are also represented as tables.

  • Arrays indices start at one, not zero

Keep in mind that array indices in Lua start at one (1):

 local t = { "one", "two", "three" }[0])) --> nil[1])) --> one

Key `0` does not belong to the array part of the table:

 t = { }
 t[0] = "zero" --> 0
 t[1] = "one" --> 1
  • Holes in arrays

The table length (i.e. array size) definition in Lua is a bit unusual and worth reading carefully:

 The length of a table `t` is defined to be any integer index `n` such that `t[n]` is not `nil` 
 and `t[n+1]` is `nil`; moreover, if `t[1]` is `nil`, `n` can be zero. For a regular array, with 
 non-nil values from `1` to a given `n`, its length is exactly that `n`, the index of its last 
 value. If the array has "holes" (that is, `nil` values between other non-nil values), then 
 `#t` can be any of the indices that directly precedes a `nil` value (that is, it may 
 consider any such `nil` value as the end of the array).

From the Lua manual –

This affects not only length operator `#`, but any code that uses it directly or indirectly, like functions `unpack()` and `table.concat()`.

 local t = { "one", "two", "three", "four" } --> 4
 t[3] = nil -- Make a hole -- May print either 2 or 4

In general, avoid making “holes” in tables that you use as a linear arrays‚ unless you know what you’re doing.

Where to find more information

Information available at


Official Lua documentation


Lua community


Feedback and Knowledge Base