Basic

_G

Description: Global variable that holds the global environment (all global functions/variables)

Example:

print(_G.tostring(true)) -- tostring is global, which makes it accessible via _G as well

setfenv

Description: sets the environment for a function to use, rather than _G.

Usage: setfenv(f)

Example:

function x()
    zzz.x = 5
    print() -- will not work here, the environment is not _G.
end

setfenv(x, {zzz = {x = 10}})

getfenv

Description: returns the environment in use by given function or stack level, default 1.

Usage: getfenv([f])

Example:

setmetatable

Description: sets the metatable for given table

Usage: setmetatable(table, metatable)

Example:

getmetatable

Description: gets the metatable of specified table, returning the __metatable metamethod's value if applicable

Usage: getmetatable(table)

Example:

rawset

Description: sets a value in the table at specified index without invoking any metamethods

Usage: rawset(table, index, value)

Example:

rawget

Description: returns a value from a table at given index without invoking any metamethods

Usage: rawget(table, index)

Example:

rawequal

Description: compares two values using == without invoking any metamethod

Usage: rawequal(v1, v2)

Example:

pairs

Description: iterator to traverse over a table in any order possible

Usage: pairs(t)

Example:

ipairs

Description: iterator to traverse over a table in sequence

Usage: ipairs(t)

Example:

loadstring

Description: loads a Lua chunk

Usage: loadstring(string [, chunkName])

Example:

loadfile

Description: loads a Lua chunk from specified file, or from standard input if filename is not specified

Usage: loadfile([filename])

Example:

next

Description: returns the next key, value pair in table starting from specified index, otherwise index, is nil

Usage: next(table [, index])

Example:

pcall

Description: calls a function in a protected state, returning any errors if they happen, otherwise returns true if successful plus the returned values from f

Usage: pcall(f, ...)

Example:

xpcall

Description: calls a function in a protected state, using err as the error handler and returning true if no errors happen, otherwise returns false plus the result from err

Usage: xpcall(f, err)

Example:

Output:

unpack

Description: unpacks a table in sequence, starting from i and ending with j (1, #table respectively by default), returning all values from it

Usage: unpack(table [, i [, j]])

Example:

type

Description: returns the data type of given value

Usage: type(value)

Example:

tonumber

Description: converts value to a number if possible

Usage: tonumber(val [,base])

Example:

tostring

Description: converts value to a string

Usage: tostring(val)

Example:

Last updated