.create()

Brag2gr8's avatar
Published Oct 6, 2023
Contribute to Docs

The .create() function generates a new table with a specified value repeated a given number of times.

Syntax

table.create(size, value)
  • size: A number representing the number of elements in the table.
  • value: A string representing the value each element will be.

Note: When value is omitted or nil, the table will be returned empty but will have preallocated space for the size specified. Preallocation is done on the array portion of the table, making table.create counter-productive on dictionaries.

Example

Consider the following table:

local newTable = table.create(3, "Hello")
for i = 1, 3 do
print(newTable[i])
end

After executing this code, it will produce the following output:

Hello
Hello
Hello

All contributors

Contribute to Docs

Learn Luau on Codecademy