Search code examples
robloxluau

RemoteFunction returns anything but a function object to client


I have a module script:

local module = function()

print("Hello")

end

return module

A local script:

task.wait(3)
local module = workspace.RemoteFunction:InvokeServer(workspace.ModuleScript)
print(module)

And a server script:

local module = require(workspace.ModuleScript)

workspace.RemoteFunction.OnServerInvoke = function(plr, m: ModuleScript)

print(require(m))
return require(m)

end

They all have legacy run context, module and server scripts are in workspace, local script is is StarterGUI

Running the game gives this output:

12:46:56.339  function: 0x3bab43ef5da9cba0  -  Server - Script:4
12:46:56.348  nil  -  Client - LocalScript:3

Making module script return anything but a function object works, returning a function object sets it to nil on the client. Why? I tried to read the official docs but im either a bad reader or they don't say "RemoteFunctions cannot return function objects to a client" anywhere


Solution

  • You cannot pass functions from client to server or reversed as the documentation suggests:

    Functions included as arguments for a RemoteEvent or RemoteFunction will not be replicated across the client-server boundary, making it impossible to pass functions remotely. Instead, the resulting argument on the receiving side will be nil.

    https://create.roblox.com/docs/scripting/events/remote#passed-functions