Why is the table-metatable bond being broken?

So, I have this local script which invokes a remote function. The server returns a table. This table is an object and this table has a meta table connected to it. However, when I use getmetatable() to show me the meta table, it returns nil. This table displays all the variables it has, but attempting to call any function will lead to an error. Am I doing something wrong?

1 Like

When a table is sent over a remote or bindable instance it needs to be serialized by the sender and then deserialized by the receiver. Whatever you send through will be a copy of the original table. In addition, there are types which aren’t supported and will be converted to nil such as functions, mixed-tables, and any metatables attached to another table.

2 Likes

You can use this to send almost any table through the network. I wrote it up because I was having problems transferring deep tables.

code for serialization:

function  module.tableSerialize(myTable)
    local tempTable = {}
    local i =1
    for k,v in pairs(myTable) do 
        if type(v)=="table" then 
            v=module.tableSerialize(v)
        end
        tempTable[i] = {k,v}
        i+=1
    end
    return tempTable
end

function  module.tableDeserialize(myTable)
    local tempTable = {}
    for _,t in pairs(myTable) do 
        if type(t[2]) == "table" then 
            t[2] = module.tableDeserialize(t[2])
        end
        tempTable[t[1]] = t[2]
    end
    return tempTable
end
function doingThingsOnServer() 
   local myTable = {}
   setmetaTable(myTable,{})
   return module.tableSerialize(myTable) , module.tableSerialize(getmetatable(myTable))
end

--on client
local tab, metatab = doThings:InvokeServer()
tab = module.tableDeserialize(tab)
setmetatable(tab, module.tableDeserialize(metatab))
1 Like

Thank you both. I was also wondering whether this works for the functions which are members of classes too.

The best way to handle this situation is to have the same metatable on the other side of the RemoteFunction and reassign it there.

1 Like

Ok, but will it be easily exploitable?

I did some testing and you can’t send functions. So I’m also going to wager that most meattables you wont be able to send

1 Like

Not more than sending things normally. Might be easier to steal the metatable’s code, but I wouldn’t worry about that. They can only steal LocalScripts anyways, and if they tried they would only get half of a working game.

Thank you guys. Its been a great help