Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

2 comments

0

Functions cannot be sent in remote objects.

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"

1 answer

✓ Accepted answer

This answer explains why you can't serialize[^1] 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

This discussion is preserved as it was. New answers, comments, and votes are closed.