FilteringEnabled breaks a lot of my local code. Instead of rewriting all of my scripts (many of which exceed 500 lines,) I'm looking to make a workaround that passes certain functions to the server.
In principle, the local script passes the function and its environment table through a RemoteEvent
. The server then receives the function and executes it. The server code looks like this:
script.RemoteEvent.OnServerEvent:Connect(function(client, func, env) --func is a function passed by the client --env is getfenv(func) according to the client script setfenv(func, env) func() end)
However, when I test this code, an error is thrown whenever I try to use an environment variable such as print
. The data in the environment table seems to be lost in transfer, leaving env
as an empty table. Further research indicated that tables with mixed indices cannot be passed via RemoteEvent
or RemoteFunction
. Encoding the table with HttpService.JSONEncode
failed for the same reason (even though it seems to me that environment tables are only indexed with Strings.)
I've tested this on both Play (with character) and a real server.
Is there any way that I can pass a function's environment table from client to server? If not, is there a more effective workaround to my problem?
Make a remote function. Add a script inside it.
Server Version:
script.Parent.OnServerInvoke:connect(function() -- Code here end)
Client Version:
script.Parent.OnClientInvoke:connect(function() -- Code here end)
Now to run it do this. Server:
script.Parent:InvokeClient()
Client:
script.Parent:InvokeServer()
Can work with Remote Event and Metatables can not be passed through remote functions or events.