coding help
Icon Magnifying Lens LR24
This page is in need of attention because it may be written by someone new to Lua. If you know Lua well, maybe you'd like to help.
Please improve this page or discuss this issue on the talk page.


Lua is a lightweight programming language. It is used on MediaWiki wikis, including Fandom wikis, and also in other projects (e.g. game development).[1][2] Fandom wikis have Lua enabled by default.[3]

On wikis, Lua is often used in templates. The Scribunto extension enables it. It's more powerful than parser functions, giving you more options.[1]

Icon Books Blue LR25
Note
This article assumes you know programming basics like comments, for loops, and boolean logic.
If you're new to programming, take it slowly. Try reading about these conventions for help understanding them.

What Lua looks like

Lua source code goes into pages called modules. Each module returns a Lua table of functions.

Modules are called by {{#invoke:}}. When calling a specific function, it might look like {{#invoke:Module|Function name|parameter 1 (if applicable)|parameter 2 (if applicable, etc.)}}

Here's an example of Lua code for saying "Hello, world!"[1]

local p = {}                -- This local variable holds our functions. We usually name it "p"
p.sayHi = function( frame ) -- Add a function named "sayHi" to "p". "frame" keeps our parameters

    local message = "Hello, world!"  -- Let's make a local variable

    return message      -- The result of the function is our "message" variable
end         -- end of the function "sayHi"

return p    -- Returning "p" makes our functions accessible.

On Fandom, you create modules by making pages named Module:ModuleName. Beneath your code is an area called the "Debug console." There, you can run print() functions, like print("Hello, world"). After hitting the enter key, the output should display.

How Lua works

Here are things to know:[1][4]

Lua will convert string and number types for calculations when possible. For example, 1 + "2" will give you 3, even though "2" is a string.[1]

Boolean logic

See also: Boolean logic

Lua includes boolean logic.[4]

Only nil and false are falsy. Things like 0 and "" (empty string) will be read as true![4]

If, for, etc.

Lua uses English words as operators. If you typically use languages like JavaScript, it may take time to get used to the lack of parentheses and brackets.

Here's an example of an if/else flow:[4]

-- if, elseif, and else
if chocolateCount > 0 then
    print("I have chocolate!")
elseif chocolateCount == 0 then
    print ("I do not have chocolate.")
else
    print ("Something has gone very wrong here.")

You also have for loops.[4]

-- By default, your counter will increment by 1 each time.
for i = 0, 5 do    -- Inclusive
    print("I will get another piece of chocolate.")
    chocolateCount++

-- You can add a third parameter to specify how to change your counter each time.
for i = 5, 0, -1
    print("I will eat a piece of chocolate.")
    chocolateCount--

Functions

Functions are similar to JavaScript functions: they take in parameters and return values. Rather than a closing curly brace, you write "end" at the end.[4]

local function hello(name)
    return "Hello, " .. name .. "!"
end

If you don't write "local" before your function name, it'll be global.

Recursion is allowed.

Tables

Lua calls its arrays "tables."[4] They involve key-value pairs.

myTable = {greeting = "Hello", pi = 3.14, [123] = "counting"}
print(myTable.greeting)     -- Prints "Hello"
print(myTable.[123])        -- Prints "counting". Any non-string key needs to be enclosed in brackets

You can build tables to function similarly to classes, including inheritance. You can iterate through a table:[4]

for key, val in pairs(myTable) do
  print(key, val)
end

See also

External links

References

  1. 1.0 1.1 1.2 1.3 1.4 Lua/Overview, MediaWiki
  2. Lua Programming, WikiBooks
  3. Help:Lua, Community Central
  4. 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 Learn Lua in 15 Minutes, Tyler Neylon