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

How to pass function environment from client to server?

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?

2 comments

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.

0

As a note, metatables don't get passed.

1 answer

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.

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