When I modify a table that is not frozen, it throws an error?

I modified ‘CopiedTable.Common’, yet I get an error which makes it look like I modified ‘shopWithRarities.Common’.
The error is ‘Attempt to modify a readonly table’
How can I avoid this?

local CopiedTable = {}
for i,v in pairs(shopWithRarities) do
	CopiedTable[i] = v
end
		
table.freeze(shopWithRarities.Common)
		
for i = 1,6 do
	local pseudoRandom = randomGeneration:NextInteger(0,1000)
	
	if pseudoRandom >= 450 then --55%
		if #CopiedTable.Common > 0 then
			local itemAdded = CopiedTable.Common[randomGeneration:NextInteger(1,#CopiedTable.Common)]
			table.insert(itemsForSale,
				itemAdded
			)
			table.remove(CopiedTable.Common,table.find(CopiedTable.Common,itemAdded)) --Attempt to modify a readonly table

I think it might be because of this.

local a = {v = false}
local b = a

a.v = true

print(b.v) --Prints true

Since I can’t find an “unfreeze” method I’m assuming if you call table.freeze again it’ll “unfreeze” making it writable to.


https://developer.roblox.com/en-us/api-reference/lua-docs/table

local a = {true} local b = {} for i,v in pairs(a) do b[i] = v end print(a==b)

this prints false, so it should not be referencing each other?

invalid argument #1 to ‘freeze’ (table is already frozen)

Why exactly do you need it to be read-only in the first place? I can’t understand a reason for this usage.

its for debugging, im trying to only change CopiedTable.Common, not ShopWithRarities.Common

You need to clone the whole Common table as well, otherwise Lua will use the reference to the frozen table.

1 Like
local a = {{v = false}}
b = a[1]

a[1].v = true

print(b.v) --prints true

Oh thank youuuuuuuuuuuuuuuuuuu

local tables = {}
table.freeze(tables)
print(table.isfrozen(tables)) --true
table.freeze(tables) --Errors: 'table is already frozen'
print(table.isfrozen(tables))