Skip to content
DiagnosticsReferenceNew · 0.744

replacetable

Replace a local table's contents while preserving its identity.

Details#

Removes the target table's existing keys, then shallow-copies the source table's entries into that same target object. Existing references to the target continue to refer to it. Nested tables and other reference values are shared with the source rather than cloned.

Signature#

Luau
replacetable(target: table, source: table): table

Parameters#

ParameterTypeDescription
targettableMutable local table whose entries will be replaced.
sourcetableLocal table containing the replacement entries.

Returns#

The original target table. A non-table argument raises a type error.

Examples#

Update local settings without breaking references

Luau
local settings = { theme = "dark", oldOption = true }
local sameSettings = settings
local replacement = { theme = "light", fontSize = 16 }
local result = replacetable(settings, replacement)
assert(result == settings and sameSettings == settings)
print(settings.theme, settings.fontSize) -- light 16
print(settings.oldOption) -- nil