Lua templating/Basics

Lua is implemented in MediaWiki wikis using the Scribunto/Lua extension and stored in resource pages using the Module: namespace.

Create Your First Lua Script
To create your first Lua script: local p = {} function p.hello return 'Hello!' end return p
 * 1) Navigate to Module:Sandbox.
 * 2) Clear all existing code.
 * It's a sandbox. Everyone is free to play in the sandbox.  But if you find another user is actively editing the sandbox at the same time, you may also use Module:Sandbox/Username, where Username is your Wikiversity username.
 * 1) Add the following code and save the page:

Test Your First Lua Script
To test your first Lua script:
 * 1) Navigate to either the Module_talk:Sandbox page, the Sandbox page, or your own user or sandbox page.
 * 2) Add the following code and save the page:

The result should be: Hello!

Edit Your First Lua Script
To edit your first Lua script:
 * 1) Return to the Module:Sandbox.
 * 2) Edit the line with   and add your name inside the single quotes.  You should end up with something like.
 * 3) Save the page.
 * 4) Return to the sandbox test page you used above to test your changes.  Using the module's talk page is very convenient for quick testing.
 * 5) Refresh the page to see your name returned from the script.

The result should be similar to: Hello Lua!

Understand Your First Lua Script
Now that you see what the script does, it's time to understand how it works.
 * 1)   creates a local table or array for your code and names it.
 * 2)   adds a function named   to the table.  Functions can be invoked (called) by name from outside the module.
 * 3)   returns the string   when the function is invoked (called).
 * 4)   ends the function.
 * 5)   returns the code table to whatever process loads this Lua module.

The code that runs the script includes:
 * 1)   invokes (calls) a Lua module.
 * 2)   specifies the name of the module to be loaded.
 * 3)   specifies the name of the function inside the module that is to be invoked (called).

Conclusion
Congratulations! You've now created, tested, edited, and understood your first Lua script.