Help passing reference to a function over RemoteEvent

I want to be able to tell clients through a RemoteEvent to run a specific function in ReplicatedStorage. I have many functions that will be run, these are all visual effects in a game with MANY of them, so having an event for each one is not a good solution.

I have looked into getfenv() but when i pass the resulting variable over a remote, the client gets nil.
this is how i tried it:

-- on server
local effectFunction = getfenv(module.SomeEffect)

-- on client when received through remote event
print(effectFunction) -- nil

Does anyone know how to do this?

maybe dont use

and pass the function directly

1 Like

You can’t pass program environments as objects to parameters since that’d break multiple roblox systems and be pretty bad if the wrong people had access to it. I don’t understand why you’re not using a module for this, or simply just passing the function as Qin rightfully suggested.

1 Like

I don want to us getfenv, it already didnt work for me.

As the title say, i just want to tell a client from the server a function to run that it already has.

so to be clear:

  1. client has about 100 possible functions to run, these are in various module scripts.
  2. server wants all the players to run a function that they already have access to.
  3. server sends :FireAll() with instructions on what function each client should run.

now it very easy to add the module script to the arguments passed with :FireAll()

like so:

-- OnServer
 SomeEvent:FireAll(someModuleScript)

this will pass a reference to the module instance. As long as the server and client both have access to it, such as in ReplicatedStorage, this works fine. Its not actually passing the module or the function in it, its just passing a reference to that module.

then on the client, we can simply require the module referenced. Easy. But what if we want the server to also tell the cleint to run a specific function inside that module?

I have not found an elegant way to pass the name of the function inside a module that should be run

you cannot pass function over remote events, and besides I wouldn’t do that because the client already has the modules and functions it needs, I just want the server to be able to tell the client WHICH function to run

Functions cannot be sent through remotes or bindables if it were to cross VMs (in your case, across the server and client)

Secondly don’t use getfenv. Its heavily advised that you don’t in Luau since you lose a slew of optimisations that they’ve implemented.

To answer your actual question: Create a RemoteEvent that passes the string of the method you want to run, along with any arguments you want to call it with

Server:

RemoteServer:FireClient(LocalPlayer, "MethodYouWantToRun", ...)

Client:

RemoteServer.OnClientEvent(methodToRun, ...)
  local getMethod = module[methodToRun]
  if getMethod then
    getMethod(...)
  end
end)
1 Like

this is how i do it now, and it works great. The reason i brought this up in the first place was that I showed some people my code and they thought it was weird. I thought there might be some better way because it felt a little weird using a string but it does work fine.