Codectionary / Developer documentation / Luau

Luau preallocated arrays

table.create preallocates the array portion of a table. Providing a non-nil fill value creates entries, while omitting it only reserves capacity. This function is a Luau addition.

Syntax

local values = table.create(count, initialValue)

Examples

Luau preallocated arrays

Run as a standalone current Luau script, or as a Script in Roblox Studio ServerScriptService. No Roblox engine APIs are required; type-annotated syntax is not standard Lua.

local reserved = table.create(3)
local filled = table.create(3, 0)
print(#reserved, #filled)
for i = 1, 3 do reserved[i] = i * i end
print(table.concat(reserved, ","))
  1. Output: 0 and 3, then 1,4,9. Reserved capacity is not the same as existing elements.

Best practices

  • Use this for known array capacities, not dictionaries. A table used as the fill value is the same shared table in every slot.

Runtime & versions

Luau host; Roblox engine examples require Roblox Studio. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Luau documentation

Specifications & further reading