Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why are my functions disappearing after being sent through a remote function?

Asked by 7 years ago

(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 :)

0
Functions cannot be sent in remote objects. Pyrondon 2089 — 7y
0
OnServerInvoke is a callback function, which means you cannot hook it to an event like that. You have to do "function remote.OnServerInvoke(player) end" NewVoids 97 — 7y

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
7 years ago

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.


  1. turn into bytes that can be sent over the network 

Ad

Answer this question