Library
table
A library of table functions.
About table
This library provides generic functions for table/array manipulation,
providing all its functions inside the global table variable. Most
functions in the table library assume that the table represents an
array or a list. For these functions, the "length" of a table means the result
of the length operator.
Functions 15
clear
(table: table) → ()Sets all keys in the given table tonil.Sets the value for all keys within the given table to
nil. This causes the#operator to return0for the given table. The allocated capacity of the table's array portion is maintained, which allows for efficient re-use of the space.local grades = {95, 82, 71, 92, 100, 60} print(grades[4], #grades) --> 92, 6 table.clear(grades) print(grades[4], #grades) --> nil, 0 -- If grades is filled again with the same number of entries, -- no potentially expensive array resizing will occur -- because the capacity was maintained by table.clear.This function does not delete/destroy the table provided to it. This function is meant to be used specifically for tables that are to be re-used.
clone
(t: table) → tableReturns a shallow copy of the provided table.Returns an unfrozen shallow copy of the provided table.
local original = { key = "value", engine = "Roblox", playerID = 505306092 } local clone = table.clone(original)concat
(t: Array, sep: string = , i: int = 1, j: int) → stringReturns the given range of table elements as a string where each element is separated by the given separator.Given an array where all elements are strings or numbers, returns the string
t[i] ... sep ... t[i+1] ... sep ... t[j]. The default value for sep is an empty string, the default foriis 1, and the default forjis #t. If i is greater thanj, returns the empty string.create
(count: number, value: Variant) → tableReturns a new table populated with many instances of the specified value.Creates a table with the array portion allocated to the given
numberof elements, optionally filled with the givenvalue.local t = table.create(3, "Roblox") print(table.concat(t)) --> RobloxRobloxRobloxIf you are inserting into large array-like tables and are certain of a reasonable upper limit to the number of elements, it's recommended to use this function to initialize the table. This ensures the table's array portion of its memory is sufficiently sized, as resizing it can be expensive. For small quantities this is typically not noticeable.
find
(haystack: table, needle: Variant, init: number) → VariantReturns the index of the first occurrence ofneedlewithinhaystackstarting frominit.Within the given array-like table
haystack, find the first occurrence of valueneedle, starting from indexinitor the beginning if not provided. If the value is not found,nilis returned.A linear search algorithm is performed.
local t = {"a", "b", "c", "d", "e"} print(table.find(t, "d")) --> 4 print(table.find(t, "z")) --> nil, because z is not in the table print(table.find(t, "b", 3)) --> nil, because b appears before index 3freeze
(t: table) → tableMakes the given table read-only.This function makes the given table read-only, effectively "freezing" it in its current state. Attempting to modify a frozen table throws an error.
This freezing effect is shallow, which means that you can write to a table within a frozen table. To deep freeze a table, call this function recursively on all of the descending tables.
insert
(t: Array, pos: number, value: Variant) → ()Inserts the provided value to the target position of the array.Inserts
valueinto arraytat positionpos, shifting up the elementst[pos], t[pos+1], ..., t[#t]by one position. The position must be between1and#t(inclusive) for the shift to occur; the value is then written att[pos].local t = {"a", "b", "c"} table.insert(t, 2, "new") print(table.concat(t, ", ")) --> a, new, b, cinsert
(t: Array, value: Variant) → ()Appends the provided value to the end of the array.Appends
valueto the end of arraytat position#t + 1. Unlike the positional overload, no existing elements are shifted, making this the most efficient way to grow an array by one element.local t = {"a", "b", "c"} table.insert(t, "d") print(table.concat(t, ", ")) --> a, b, c, disfrozen
(t: table) → boolReturnstrueif the given table is frozen andfalseif it isn't frozen.This function returns
trueif the given table is frozen andfalseif it isn't frozen. You can freeze tables usingtable.freeze.maxn
(t: table) → numberReturns the maximum numeric key of the provided table, or zero if the table has no numeric keys.Returns the maximum numeric key of the provided table, or zero if the table has no numeric keys. Gaps in the table are ignored.
move
(src: table, a: number, b: number, t: number, dst: table = src) → tableCopies the specified range of elements from one table to another.Copies elements in table
srcfromsrc[a]up tosrc[b]into tabledststarting at indext. Equivalent to the assignment statementdst[t], ..., dst[t + (b - a)] = src[a], ..., src[b].The default for
dstissrc. The destination range may overlap with the source range. Returnsdstfor convenience.local sourceTable = {4, 5} -- Table of data to copy from local destTable = {1, 2, 3} -- Table to add copied data to table.move( sourceTable, -- Source table 1, -- Index to start from in source table #sourceTable, -- Index up to (and including) from source table #destTable + 1, -- Index within destination table to move data into destTable -- Destination table ) print(destTable) --> {1, 2, 3, 4, 5}pack
(values...: Variant) → VariantReturns a new table containing the provided values.Returns a new table with all arguments stored into keys 1, 2, etc. and with a field "n" with the total number of arguments. Note that the resulting table may not be a sequence.
local t = table.pack(1, 2, 3) print(table.concat(t, ", ")) --> 1, 2, 3remove
(t: Array, pos: number) → VariantRemoves the specified element from the array, shifting later elements down to fill in the empty space if possible.Removes from array t the element at position pos, returning the value of the removed element. When pos is an integer between
1and#t, it shifts down the elementst[pos+1], t[pos+2], ..., t[#t]and erases elementt[#t]. If the pos parameter is not provided, pos defaults to the length of the table removing the last element.sort
(t: Array, comp: function = nil) → ()Sorts table elements using the provided comparison function or the<operator.Sorts elements of array t in a given order, from
t[1]tot[#t]. Ifcompis given, then it must be a function that receives two elements and returns true when the first element must come before the second in the final order.The error
invalid order function for sortingis thrown if bothcomp(a, b)andcomp(b, a)returntrue.If
compis not given, then the standard Luau operator<is used instead.unpack
(list: table, i: number = 1, j: number = #list) → TupleReturns all elements from the given list as a tuple.Returns the elements from the given list. By default,
iis 1 andjis the length oflist.Note that this same functionality is also provided by the global
LuaGlobals.unpackfunction.
Reference text from the Roblox Creator Documentation, licensed CC BY 4.0, reorganized by rbxtoolshub.com · Studio 0.743.0.7431133.