freeze()

KyraThompson's avatar
Published Sep 1, 2023Updated Mar 2, 2024
Contribute to Docs

The .freeze() function in Luau freezes the provided table and makes it read-only.

Syntax

table.freeze(f)

The .freeze() function attempts to freeze a specified non-frozen table named f. The function will fail if f is not a table or is already frozen.

Example

Consider the following table:

local frozen_table = table.freeze({
"🍎",
"🍌",
"🍇",
"🍓",
"🍉"
})

The variable frozen_table is instantiated with the .freeze() method to make the contents read-only.

Now, if an element is added:

frozen_table[1] = "🍔"

The code will return the following output:

Error: Frozen table cannot be modified

The error is thrown because the method has made the table read-only, so the contents cannot be modified.

All contributors

Contribute to Docs

Learn Luau on Codecademy