getn()
The .getn()
function in Luau returns the number of elements in a table by iterating over the table and counting the elements, while ignoring any nil elements. This function is a handy tool for various table-related tasks, including checking the table’s length, allocating memory for a table, and determining if a table is empty.
Note: This function has been deprecated. Applying the
.getn()
function will result in an error. Use#t
instead.
Syntax
table.getn(t)
The table.getn()
function takes only one argument, t
, the table to retrieve the length of. It returns the number of elements in the table, as an integer.
Example 1
local table = {1, 2, 3, 4, 5}local count = table.getn(table)print(count)
This code will print the following output:
5
Example 2
local table = {1, 2, 3, nil, 5}local count = table.getn(table)print(count)
4
Example 3
local table = {1, 2, 3, 4, 5}local count = table.getn(table)for i = 1, count doprint(table[i])end
This code will print the following output:
12345
Example 4
Here is an example of how to use the table.getn()
function to check if a table is empty:
local table = {}if table.getn(table) == 0 thenprint("The table is empty.")end
This code will print the following output:
The table is empty.
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.