Encoding functions to string and back?

I was wondering if it was possible to encode functions to string and back with Roblox Lua.

This is mainly for organizational purposes so I can store client and server functions in one module script in serverstorage and encode and send the client the functions they need, and no more.

JSONEncode does not work:

HttpService = game:GetService("HttpService")
mytab = {hi = function() print"Hello World!" end }
print(HttpService:JSONEncode(mytab))

>>{"hi":null}
1 Like

This is not possible nor recommended. If youā€™re desiring to share code across Server and itā€™s Clients, just use a ModuleScript placed in ReplicatedStorage. Itā€™ll accomplish the same thing

9 Likes

uGH

But I hate the idea of storing server functions and client funtions in seperate module scripts for each ā€˜spellā€™ I am making.

1 Like

Well regardless, you donā€™t want to expose the server side part of it, assuming youā€™re doing sanity checks on server, people could see that code and exploit it

Itā€™ll just be uglier than neccessary, but I guess Iā€™ll have to

1 Like

I mentioned something about why the limitations are here (itā€™s about metatables but the reason is because of functions): Metatables arenā€™t being sent with the tables they are assigned to?

1 Like

When I say use a ModuleScript in ReplicatedStorage, Iā€™m thinking more of a ā€œfoundationā€ or ā€œcoreā€ module for both Server and Client for maintaining a similar base for easier management of code. Definitely do not put functions which are dedicated to the Server in this Module. If any, put the core module along with any utilities/libraries which need to be shared, nothing else.