luaguides

Reference

Core Functions

Built-in functions available in every Lua program.

  1. assert

    Assert a condition in Lua with assert. Check types, validate function returns, and when not to use assert for user-facing errors.

  2. collectgarbage

    Control Lua's garbage collector: run cycles, tune pause and step multiplier, switch between incremental and generational modes, and query memory use.

  3. dofile: Open and Execute a Lua File

    The dofile function in Lua 5.4 opens and executes a file as a chunk, returning all values the chunk returns. Errors propagate to the caller.

  4. error

    Raise a runtime error in Lua. Stop execution, propagate errors up the call stack, and use the level parameter to blame the right caller.

  5. getmetatable

    getmetatable returns the metatable of a Lua value, or its __metatable guard value when one is set. Works on tables, strings, userdata, and every other type.

  6. ipairs

    Iterate over sequential Lua tables with ipairs. This iterator traverses array-style entries in order, stopping at the first nil gap.

  7. load

    The Lua load() function compiles a chunk from a string or reader function without running it, returning the compiled function or a syntax error.

  8. loadfile

    Load a Lua chunk from a file and return it as a function, without executing it. Supports text or binary mode and a custom _ENV.

  9. next

    Traverse Lua tables in arbitrary order with next(). Returns the next key-value pair or nil when the table is empty.

  10. pairs

    Iterate over key-value pairs in Lua tables with pairs. Unlike ipairs, pairs iterates all entries including non-sequential keys and handles nil values correctly.

  11. pcall

    Call a function in protected mode. If the function raises an error, pcall catches it and returns false with the error message instead of propagating the error.

  12. print()

    Output one or more values to the console. print is the simplest way to inspect values during development and debugging in Lua.

  13. rawequal

    rawequal tests whether two Lua values are the same object in memory without invoking __eq metamethods. Use for identity checks in comparison and table-key code.

  14. rawget

    rawget() retrieves a table value directly, bypassing the __index metamethod to give you the actual stored data without metatable fallbacks.

  15. rawlen

    The rawlen function returns the length of a table or string without invoking __len metamethods, giving you the raw sequence length directly from the value.

  16. rawset

    rawset() writes a value directly into a table, bypassing the __newindex metamethod so assignments hit raw storage without custom handlers.

  17. require

    The Lua `require` function loads a module from the file system, a C library, or the package.preload table, caching the result in package.loaded for reuse.

  18. select

    Select elements from a variadic argument list. Get all arguments after a position, or count the total arguments with the '#' selector.

  19. setmetatable

    The setmetatable function assigns a metatable to a table, enabling custom behavior for operators, indexing, and function calls through metamethods.

  20. table.unpack

    Lua 5.4's table.unpack returns list elements as multiple values, the standard way to spread a table into a function call or multi-assignment.

  21. tonumber

    The tonumber() function converts strings and numbers to numeric values in Lua, supporting base 2-36 parsing for binary, hex, and octal input.

  22. tostring

    The tostring() function converts any Lua value to a string — essential for debugging, logging, and building strings with concatenation of mixed types.

  23. type()

    Return the type name of any value as a string. Use type() to check what kind of value you're working with at runtime.

  24. xpcall

    xpcall calls a function with a custom error handler. The handler gets the raw error value, not a string. Essential for logging and structured error handling.