The functions listed below aren't in any specific library, but are global in the script's environment. These are general purpose functions that don't fit into any of the other categories.
Variant assert(Variant v)
Throws an error if v
. Returns the value passed as v
if the assert passes.Example:
void assert(Variant v, string message)
Throws an error if 'v' is false
, with the error message set as message
. Returns the value passed as v
if the assert passes.Example:
Variant collectgarbage(string option)
Performs an operation on the Lua garbage collector based on the specified option.
Roblox's Lua sandbox only allows the "count" option to be used, so none of the other standard options are available.
option returns the total memory in use by Lua (in Kbytes).Example:
void error(string message)
Terminates the last protected function called and outputs message as an error message. If the function containing the error is not called in a protected function (pcall), then the script which called the function will terminate.
The error function itself never returns and acts like a script error.
void error(string message, int level)
Terminates the last protected function called and outputs message as an error message.
Usually, error adds some information about the error position at the beginning of the message. The level
argument specifies how to get the error position. With level 1 (the 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 a level 0 avoids the addition of error position information to the message.Example:
A table that is shared between all scripts in one instance of Roblox. Scripts can use this to share data, including functions, between them.
- There are multiple copies of _G in Roblox's Lua implementation, separated by their context level.
Returns amount of dynamic memory in use.Example:Notes:
- This is deprecated. Use
void getfenv(Variant<int, function, nil> f)
Returns the current environment in use by the function
can be a function
or a number
that specifies the function
at that stack level: Level 1 is the function
calling getfenv. If the given function
is not a function
, or if f
is 0, getfenv returns the global environment. The f
parameter can be excluded, and it is 1 by default. When using getfenv to get the current environment of a script, it will return the same table every time within the specific thread.Example:
Variant getmetatable(Variant<table, userdata> object)
Returns the metatable of the specified object if it has one, otherwise returns nil.
If the object does have a metatable, but the metatable has a __metatable
field set, the value of __metatable
will be returned instead.Example:
function, table, int ipairs(table t)
Returns three values: an iterator function, and the table t.
This will iterate over the pairs (1,t), (2,t), ···, up to the first integer key absent from the table.
load(function func, string chunkName)
function loadfile(string path)
Loads a lua file from the specified path and returns it as a function.Example:Notes:
- This is used internally by Roblox, and doesn't really have practical use for developers.
loadstring cannot be used from a LocalScript
, however, it can be used from a Script
if loadstring is enabled through the LoadStringEnabled property of ServerScriptService.
Variant<function, Tuple<nil, string>> loadstring(string contents)
Loads Lua code from a string, and returns it as a function.
To load and run a given string, use the idiom
- Unlike standard Lua 5.1, Roblox's Lua cannot load the binary version of Lua using loadstring.
Creates a blank userdata with an empty metatable.
Variant, Variant next(table t)
Returns the first key/value pair in the array.
Variant, Variant next(table t, Variant lastKey)
Returns the next element in the array based on a key that was previously retrieved from a call to next.Example:Notes:
- The order in which the indices are enumerated is not specified, even for numeric indices. To traverse a table in numeric order, use a numerical for or the ipairs function.
- The behavior of next is undefined if, during the traversal, you assign any value to a non-existent field in the table. You may however modify existing fields. In particular, you may clear existing fields.
Returns how many seconds since the epoch (1 January 1970, 00:00:00), under UTC time.
- This differs from tick(), because tick uses the current system time rather than UTC time.
- In theory, this value will be the same on all systems, but in practice it will often differ with some seconds.
- os.time() should be used for cases where you want to get a time difference between two visits - think about giving players a bonus for coming back every day.
int os.time(dictionary dateTable)
When called with a date table, os.time will return the timestamp which belongs to that date table.
This date table can hold the following fields:
|| Valid values
|| Default values or mandatory
|| A full year number (ex. 2014)
|| True if daylight saving is on
dictionary<Variant<int, bool>> os.date(string formatString, number time)
Takes a UNIX time stamp and returns information about what date it is. Format string must be either
*t is local time (only useful on the client), and
!*t is UTC.
The returned dictionary contains the following fields (it is compatible with os.time):
|| An integer that describes the current year of the Current Era (ex. 2017)
|| An integer between 1 and 12 (starting from January) that describes the current month.
|| An integer between 1 and 7 (starting from Sunday) that describes the current week day.
|| An integer between 1 and 366 describing how many days we are into the year.|
There can be 366 days if it is a leap year.
|| An integer between 1 and 31 describing the current day of the month.
|| An integer between 1 and 24 describing the current hour of the day.
|| An integer between 0 and 59 describing the current minute of the hour.
|| An integer between 0 and 60 describing the current second of the hour.|
(60 because the function is described to indicate leap seconds, but in practice it probably doesn't).
|| A boolean describing if daylight savings time is currently active.
int os.difftime(int t1, int t2)
Returns the number of seconds from t1 to t2. In Microsoft Windows and systems based on Unix, this is equivalent to t1 − t2; this is always the case in ROBLOX.
Returns a stack trace of the current thread, which is identical to the one shown in the output when an error occurs.Example:
void debug.profilebegin(string profileName)
Marks the starting point of a custom label in the Microprofiler
while true do
--your code here
Closes the custom label that was created in this stack's Microprofiler.
function, table pairs(table t)
Returns the next function, and the passed table 't', so that the construction
for k,v in pairs(t) do
will iterate over all key–value pairs of table
- See the function next for the caveats of modifying the table during its traversal.
bool, Variant<string, Tuple<Variant>> pcall(function func, Tuple<Variant> args)
Calls the function f
with the given arguments in protected mode. This means that any error inside f
is not propagated; instead, pcall catches the error and returns a status code. Its first result is the status code (a boolean), which is true
if the call succeeds without errors. In such case, pcall also returns all results from the call, after this first result. In case of any error, pcall returns false
plus the error message.Example:
void print(Tuple<Variant> params)
Receives any number of arguments, and prints their values to the output, using the tostring function
to convert them to strings. print is not intended for formatted output, but only as a quick way to show a value, typically for debugging. For a formatted output, use string.format.Example:
bool rawequal(Variant v1, Variant v2)
Checks whether v1
is equal to v2
, without invoking any metamethod.Example:
void rawget(table t, Variant index)
Gets the real value of table
], without invoking any metamethod
table rawset(table t, Variant index, Variant value)
Sets the real value of table
] to value
, without invoking any metamethod. table
must be a table
any value different from nil
, and value
Tuple<Variant> select(int index, Tuple<Variant> args)
Returns all arguments after argument number index
int select(string cmd = "#", Tuple<Variant> args)
Returns the total number of arguments that were passed after the cmd argument.
to use select in this fashion.Example:
Variant<function, nil> setfenv(Variant<function, int> f, table fenv)
Sets the environment to be used by the given function. f can be a function or a number that specifies the function at that stack level: Level 1 is the function calling setfenv. setfenv returns the given function.
As a special case, when f
is 0 setfenv changes the environment of the running thread. In this case, setfenv returns no values.Example:Notes:
table setmetatable(table t, Variant<table, nil> setTo)
Sets the metatable for the given table
. If setTo
, the metatable of the given table
is removed. If the original metatable has a "__metatable" field, this will raise an error. This function
returns the table t
, which was passed to the function.Example:
Variant<number, nil> tonumber(Variant arg)
Attempts to convert the arg into a number in base-10.
If it cannot be converted, this function returns nil.
void tonumber(Variant arg, int base)
Attempts to convert the arg
into a number
with a specified base
to interpret the value in. The base may be any integer between 2 and 36, inclusive. In bases above 10, the letter 'A' (in either upper or lower case) represents 10, 'B' represents 11, and so forth, with 'Z' representing 35. In base 10 (the default), the number
may have a decimal part, as well as an optional exponent part. In other bases, only unsigned integers are accepted.Example:
string tostring(Variant e)
Receives an argument of any type and converts it to a string
in a reasonable format.Example:Notes:
- If the metatable of e has a "__tostring" field, then tostring calls the corresponding value with e as argument, and uses the result of the call as its result.
- For complete control of how number are converted, use string.format.
string type(Variant v)
Returns the type of its only argument, coded as a string
. The possible results of this function are "nil" (a string
, not the value nil
), "number", "string", "boolean", "table", "function", "thread", and "userdata".Example:
void unpack(table list, int i = 1, int j = #list)
Returns the elements from the given table. This function is equivalent to
return list[i], list[i+1], ···, list[j]
except that the above code can be written only for a fixed number of elements. By default, i
is 1 and j
is the length of the list, as defined by the length operator.Example:
A global variable (not a function) that holds a string containing the current interpreter version. The current contents of this variable is "Lua 5.1".
- This value is misleading, as Roblox actually uses a modded version of Lua 5.1.4
bool, Variant<string, Variant> xpcall(function f, function err)
This function is similar to pcall, except that you can set a new error handler.
xpcall calls function f
in protected mode, using err
as the error handler. Any error inside f
is not propagated; instead, xpcall catches the error, calls the err function
with the original error object, and returns a status code. Its first result is the status code (a boolean
), which is true
if the call succeeds without errors. In this case, xpcall also returns all results from the call, after this first result. In case of any error, xpcall returns false
plus the result from err.Example: