Can you pass a function through a remote event?

I’ve been trying to do

-- Server
local func = function()
	...
end
RemoteEvent:FireAllClients(func)


-- Client
RemoteEvent.OnClientEvent:Connect(function(func)
	func()
end

but it keeps saying (on client side) that ‘func’ is a nil value

5 Likes

No, this is not possible. Functions can’t be serialized and sent over remotes like this. You can only send data types and instance references over the network.

18 Likes

well shoot lol

1 Like

You could achieve the same effect by using a RemoteFunction or connecting a RemoteEvent to a function.

Not quite because that wouldn’t let you run arbitrary logic (i.e. pass arbitrary functions over the network).

Either way, arbitrary code execution is a thing that is to be avoided like the plague anyhow, so this is an anti-pattern altogether.

1 Like

I see.

I’m thinking about just putting the function in a local script and then cloning the local script, and placing it into each player

This is what module scripts are for. If the function is static anyway, then just place it in a module script on the client, and require that module to get access to the function.

3 Likes

if you need the function to happen on the client, then having the function already in the localscript before cloning would eliminate the need to transfer a function.

I would but I wanted to keep it on the server

If you’re running it on the client it has to be available to the client at some point in time. No point hiding this information by only cloning it in once it is needed, that seems way too complex for no benefit.

If this function contains sensitive data that the client shouldn’t be seeing, you need to rethink your architecture and what you actually should be running where.

4 Likes

Well it’s for a countdown command for my admin system. I did think about putting it in the client but I wanted to keep it as the command module’s child because I’m weird about how I organize things.

A good way to make a countdown or message system is to have a RemoteEvent in ReplicatedStorage and then a receiver script on the client so that when you want to send a message (or countdown) you can just fire the remote event with a string and a message duration or something, and then have the clients render that message using whatever method you want to use.

1 Like

Thanks for the advice :slight_smile:

No problem =)

You can actually “send functions” to the client - although its pretty unorthodox, it does work. This is done by Adonis for example in its ;run command.

You can take a look at Adonis’s MainModule (https://www.roblox.com/library/2373501710/Adonis-MainModule), and its loadstring (MainModule/Core/Dependencies/Loadstring) implementation for more details. An example is below:

local RS = game:GetService("ReplicatedStorage")
local LS = require(RS.Loadstring)
RS.RunClient.OnClientEvent:Connect(function(Str)
	LS(Str)()
end)

After that, you can then just send scripts from the server:

game:GetService("ReplicatedStorage").RunClient:FireClient(Player, "print'hi'")

For security concerns, this is usually fine if its only done Server->Client. Do not do this from Client->Server though.

13 Likes

I never knew that, Very cool.

I’m curious, what if you json encoded the function?

1 Like

No, functions cannot be encoded in JSON. The only possibility you have is moving the source string over the network and using a custom loadstring function to execute it, which you should never be doing in any use case related to game development, period.

6 Likes

Why should we never be doing this? I’m very curious.