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

How to pass function environment from client to server?

Asked by
Minifig77 190
6 years ago
Edited 6 years ago

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?

3
This isn't a great idea as it allows the client to execute any function they please which could be a SIGNIFICANT security flaw. Rewriting your code is probably the better option here, as any solution would need to use some pretty dirty hacks and again, compromise game security. thebayou 441 — 6y
0
As a note, metatables don't get passed. hiimgoodpack 2009 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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.

Ad

Answer this question