diff --git a/content/en-us/luau/tables.md b/content/en-us/luau/tables.md index 79d4c4a99..5d79a1c25 100644 --- a/content/en-us/luau/tables.md +++ b/content/en-us/luau/tables.md @@ -291,14 +291,19 @@ local clone = table.clone(original) To copy a more complex table with nested tables inside it, you'll need to use a recursive function similar to the following: ```lua -local function deepCopy(original) +-- The function used for deep copying a table +local function deepCopy(original) + -- Define the new table for the copy local copy = {} - for k, v in pairs(original) do - if type(v) == "table" then - v = deepCopy(v) - end - copy[k] = v + + -- Loop through the original table to clone + for key, value in original do + -- If the type of the value is a table, deep copy it to the key (index) + -- Else (or) the type isn't a table, assign the default value to the index instead + copy[key] = type(value) == "table" and deepCopy(value) or value end + + -- Return the finalized copy of the deep cloned table return copy end ```