FANDOM


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

Create your first Lua script

To create your first Lua script:

  1. Navigate to Module:Sandbox/Username, where Username is your Fandom username.
  2. It's a sandbox. Everyone is free to play in their sandbox.
  3. Clear all existing code. Add the following code and save the page:
local p = {}
 
function p.hello()
    return 'Hello!'
end
 
return p

Test your first Lua script

To test your first Lua script:

  1. Navigate to either the Module_talk:Sandbox page, or your own user or sandbox page.
  2. Add the following code and save the page:
{{#invoke:Sandbox|hello}}

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 return 'Hello!' and add your name inside the single quotes. You should end up with something like return 'Hello Lua!'.
  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. local p = {} creates a local table or array for your code and names it p.
  2. function p.hello() adds a function named hello to the table. Functions can be invoked (called) by name from outside the module.
  3. return 'Hello!' returns the string Hello! when the function is invoked (called).
  4. end ends the function.
  5. return p returns the code table to whatever process loads this Lua module.

The code that runs the script includes:

  1. invoke: invokes (calls) a Lua module, and loads something
  2. Sandbox specifies the name of the module to be loaded.
  3. hello specifies the name of the function inside the module that is to be invoked (called).
{{#invoke:Sandbox|hello}} Keyword 1st Parameter 2nd Parameter

Code

#invoke:

Sandbox

hello

What it does

specifies action - here load module and implement function

specifies the name of the module to be loaded

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.

Learn more

References

  1. http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual
Community content is available under CC-BY-SA unless otherwise noted.

Fandom may earn an affiliate commission on sales made from links on this page.

Stream the best stories.

Fandom may earn an affiliate commission on sales made from links on this page.

Get Disney+