Luau Find()

moha230's avatar
Published Dec 30, 2023Updated May 15, 2024
Contribute to Docs

The find() function is used to search for a specific value in a table and returns its index if found. The function returns nil if the value is not present in the table.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours

Syntax

table.find(myTable,value)
  • myTable: A table containing values where the specified value being searched is present.
  • value: This represents parameter of the value being sought after in the table.

Example

In this example, a table containing strings of letters is declared, and the table.find() function is utilized to discover the index of a sought after value within the table. If not found, it will return nil instead.

-- Create a sample table
local myTable = {"a", "b", "c", "d", "e"}
print(table.find(myTable, "d"))
print(table.find(myTable, "j"))

This example results in the following output:

4
nil

All contributors

Contribute to Docs

Learn Luau on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours