(Server Code)
remote.OnServerInvoke:connect(function(player) local obj = { name = "memer"; foo = function(self) print("Hello, world!") end } return obj end)
(Client Code)
local obj = remote:InvokeServer() print obj.name obj:foo()
The output will print obj.name
as "memer" but will then say something like foo is not a member of obj
and will fail execution. I tried to execute obj:foo()
on the server and it executed successfully but after being sent to the client it seems to keep its properties but not its functions. Is this a bug or something inherent to remote functions / tables and is there anyway to get around this?
Thanks :)
This answer explains why you can't serialize1 functions.
Since you're essentially creating a "class", I can recommend the same answer that I gave there.
Use a ModuleScript to "imbue" objects with their methods on the server.
LocalScripts should use the same ModuleScript to imbue received objects with the appropriate class (depending on the type of object received).
Since your functions really are just defined by the class, and instances of a class are defined only by their (non function) fields' values, this should be sufficient.
turn into bytes that can be sent over the network ↩