I’ve seen some posts about this saying it’s not possible to send it directly like this:
—server
local func = function()
…
end
remote:FireClient(player, func)
—client
remote.OnClientInvoke:Connect(function(func)
func()
end
But isn’t it technically possible to do this though?
—Server
local table = {}
function table.func()
…
end
remote:InvokeClient(player, table)
—Client
remote.OnClientInvoke(table)
table.func()
end
If not, can I just send a module script from server to the client? Like so:
—server module
function module.func()
…
end
—server
remote:InvokeClient(player, require(module)
—client
remote.OnClientInvoke(module)
module.func()
end
2 Likes
Functions will not be passed over the client/server boundary.
You can’t pass a function through remotes. If you want a specific function to run with a remote, you can do this:
(Client)
local t = {}
function t.method(...)
print(...) --> prints whatever was passed
end
-- 'methodName' would be the name of the function in the table that should run
-- '...' are the arguments that the server sent to the client to be passed into the function
RemoteEvent.OnClientEvent:Connect(function(methodName, ...)
local func = t[methodName] -- get the function in the table
if func then -- check if the function is in the table first
func(...) -- run the function with the provided arguments
end
end)
(Server)
RemoteEvent:FireClient(Player, "method", "Hello World")
-- "methodName" ^ Arguments ^
The table will be empty. Tables are copied into remote events (not passed by reference), and any invalid keys are cleared before they are sent.
The same applies here. You are copying the table returned by requiring the module, and anything invalid will be cleared.
Your only option is using a module script that the client has access to, and as @HugeCoolboy2007 suggested, you can pass the module script instance and the name of the function you want the recipient to call as arguments.