Skip to content

Global Functions

Description

Global Lua standard library functions and miscellaneous extensions to them.


assert

assert(expression, message)

If the first argument is false or nil, an error is thrown with the second argument as the message. Otherwise, all arguments are returned.

collectgarbage

collectgarbage(opt, arg)

Performs a garbage-collection operation. The first argument specifies the operation to perform

Modifying garbage collector settings is generally not recommended unless you know what you’re doing.

defer

defer(callback)

Registers a function to be called at lua shutdown/reload, equivalent to client.set_event_callback("shutdown", callback)

error

error(message, level)

Terminates the last protected function called and returns the message. The level argument specifies where the error function gets its information about the error position. If the function containing the error is not called in a protected function, then the script which called the function will terminate. The error function itself never returns and acts like a script error. If level is 1 (default), the error position is where the error function was called. Level 2 points the error to where the function that called error was called; and so on. Passing level 0 avoids the addition of error position information to the message.

getfenv

getfenv(stack)

Returns the environment of the function or stack level passed to it.

getmetatable

getmetatable(table)

Returns the metatable of the given table if present, otherwise it returns nil. If it does have a metatable, and the metatable has a __metatable metamethod, it returns that value instead.

ipairs

ipairs(table)

Returns three values: an iterator function, the table table, and 0. Each time the iterator function is called, it returns the next numerical index-value in the table.

When used in a generic for-in-loop, the return values can be used to iterate over each numerical index in the table.

load

load(chunk, chunk_name, mode, env)

Loads a chunk of Lua source code from a string or a reader function.

next

next(table, key)

Returns the first key/value pair in the table. If a key argument was specified then returns the next element in the table based on the key provided.

pairs

pairs(table)

Returns three values: the next function, the table, and nil. Each time the next function is called, it returns a key-value pair from the table.

pcall

pcall(func, arg1, ...)

Calls the function func with the given arguments in “protected mode”. This means that any error inside func will not propagate; instead, pcall catches the error and returns a status code and the error message. If the call succeeds without errors, it returns true followed by any values returned by func. If there is an error, it returns false followed by the error message.

print

print(...)

Logs a message to the console. Equivalent to client.log with the “Console” type.

printf

printf(format, ...)

Logs a formatted message to the console. It accepts the same parameters as string.format

rawequal

rawequal(v1, v2)

Checks whether v1 and v2 are equal without invoking any metamod.

rawget

rawget(table, key)

Gets the raw value of a table field, without invoking the __index metamethod.

rawlen

rawlen(table)

Gets the raw length of a table, without invoking the __len metamethod.

rawset

rawset(table, key, value)

Sets the raw value of a table field, without invoking the __newindex metamethod.

readfile

readfile(filename)

Reads the contents of a file and returns it as a string, or nil if the file doesn’t exist.

require

require(module_name)

Loads a Lua module.

select

select(index, ...)

Returns all the arguments passed to it after the first index. If the first index is "#", it returns the total number of arguments it received (including nil).

setfenv

setfenv(stack, table)

Sets the environment of the function to the given table.

setmetatable

setmetatable(table, metatable)

Sets the metatable for the given table. If metatable is nil, the metatable of it is removed. This function returns the table tbl which was passed to it. If tbl already has a metatable with the __metatable metamethod, calling this on table causes an error.

tonumber

tonumber(value, base)

Tries to convert value to a number. If the argument is invalid, it returns nil.

tostring

tostring(value)

Receives an argument of any type and converts it to a string. For complete control of how numbers are converted, use string.format instead.

toticks

toticks(seconds)

Converts seconds to ticks.

totime

totime(ticks)

Converts ticks to seconds.

type

type(value)

Returns the type of the given value as a string.

unpack

unpack(table, i, j)

Returns the items with numeric keys from the given table, from i to j.

writefile

writefile(filename, content)

Writes the given content to a file, overwriting it or creating it if it doesn’t exist.

xpcall

xpcall(func, err_handler, arg1, ...)

Calls func in “protected mode”. This means that any error inside func is not propagated; instead, xpcall catches the error, calls the “error handler” function passed to it, then returns back to the caller. If the first return value is true, the function call succeeded with no errors and the other return values will be the return values from the function. If the first return value is false, the second one will be the error message.