Why that doesn't work?
In lua-users wiki the say that a generic table.copy function is not guaranteed to suite all use-cases, as there are many different aspects which must be selected for the specific situation. For example: should metatables be shared or copied? Should we check userdata for a __copy metamethod? These questions (as well as many others) must be asked and answered by the developer.
So how to copy a table?
A simple function to make a copy of a table is really easy to implement. Just make a function like this.
function copyTable(t)
local t2 = {} –We define here the new table
for k, v in pairs(t) do –Don't use ipairs here because that only works with numeric inexes
t2[k] = v –Here we copy the key and the value from the original table
end
return t2 –Return the copyed table
end
–To copy the function you only have to do:
copy = copyTable(tableToCopy)
What happens with metatables?
I don't know how to use metatables, so I can't help here.
Why did you use quote instead of code for the function?
Because code in this forum doesn't highlight LUA sintax and I have a plugin to copy the code with sintax highlight from Notepad++, so I think it's better to understand the funtion than plain text with some colors that doesn't say anything.
I hope this can help you. If you have any questions, post a comment.
EDIT:
Here is a easier way and it works better (to copy a table inside of a table) but it's only compatible with ComputerCraft:
function copyTable(t)
local aux = textutils.serialize(t)
local t2 = textutils.unserialize(aux)
return t2
end