Is There Any Way To Pass A Function From Server To Client (Like A RemoteEvent)

For example;

RemoteEvent:FireAllClients(function() print("Hi") end)

-->> The client runs the passed function & prints "Hi"
RemoteEvent.OnClientEvent:Connect(function(PassedFunc)
   PassedFunc()
end

Any other alternative?

You can’t pass functions between the client and server boundary.

The closest you’d get to what you want to achieve is with RemoteFunctions.

1 Like

as mentioned above, the closest you can get is RemoteFunctions. If possible, you could also store the function in a module script (put in replicatedStorage) then when the client is called with a remoteEvent, run that particular function in it

1 Like

Yeah that’s what I’ve done previously, however from the type of game I’m working on that can very quickly cause a ton of clutter.

From my framework being able to pass functions to clients would be much more efficient. A shame, that you can’t.
Any particular reason why? I don’t see how it’d be for security as it seems safe to me as long as you sanity check stuff- which should be default behavior.

1 Like

Any particular reason why?

To be honest, it is much more reasonable if I turn that back on you - WHY do you need to send the function to the player instead of having the player already have the function then tell them when to run it?

If you desperately don’t want them to see a bit of code, put that code in a local script parented to ServerStorage or something, then have that script be cloned to the player when needed

1 Like

It isn’t about security for me. And to be honest my use case is more complicated than I care to explain lol. Just too lazy sorry. But just believe that I could very much use this functionality as one would pass any other variable to clients.

1 Like

If it isn’t about security, then you can easily put all the functions into a module script or just a local script, and tell the client which one to run, and WHICH PARAMETERS should be used, using a remote event. There is no reason, at ALL, that this is not viable.

2 Likes

I didn’t say it wasn’t viable. I have done that before like I said. But for my use case it would be more efficient to be able to pass a function to the client.

I don’t know why you’re arguing with me on what I know to be true lol. Maybe you can’t comprehend a use case for this functionality but that doesn’t mean there isn’t one.

1 Like

If you are seriously that desperate, change the function into a string, enable loadstring(), then just do loadstring() with the remote event sending the string version of the function as the param

1 Like

What is with you lol, I’m not desperate. I have workarounds, I just asked one question that if it was possible to pass a function. I don’t really know what loadstring() does but I’ve heard lots about how it’s a game security disaster. So I’d rather not use it.

Thanks for the help anyways.

1 Like