Returning a function in a Remote Function

Im developing a graphing calculator atm, where you can input any function through a string

But I’m attempting to fire that equation to the server, make it a function with parameters, and return it back

  • Server Side
game.ReplicatedStorage.Function.OnServerInvoke = function(p,equation)
--for example the equation is 'x^2'
	local function a(x)
		return loadstring('function a(x) return ' ..equation.. ' end return a('..x..')')()
	end
	print(a(5)) - this perfectly gives 25 (5^2=25)
	return a -- returning the function 
end 
  • Client
local func = game.ReplicatedStorage.Function:InvokeServer(script.Parent.Input.Text)
print(func) -- prints nil instead of 'function: 0xeac8497c435b02e6'

Theres nothing special about loadstring in this case either

game.ReplicatedStorage.Function.OnServerInvoke = function(p,equation)
	local function a(x) return x end
print(a(5)) -- 5
	return a
end 

it still returns nil on the client

Am I incorrectly doing something?

1 Like

It’s not anything you’re doing wrong; functions don’t replicate between the client and server

is there a way for the server and client to communicate with functions?

Module Script

( need more chars )

this would work fine if i werent using loadstring, loadstring I believe is exclusive to serverscriptservice, and modules cant depend on other modules, so creating a module in sss, then making another in rs relying on the one in sss wont function.

you can send the string version of the script and send it to client then loadstring it in client

You can’t use loadstring on the client and that would also be a security flaw

lua vms

-- module script
local module = {}

module.FunctionA = function(a, b)
    return a + b
end

module.FunctionB = function(a, b)
    return a - b
end

return module
-- server script
local module = require(game.ReplicatedStorage.ModuleScript)

game.ReplicatedStorage.Function.OnServerInvoke = function(player, equation)
    local data = {"FunctionA", equation, 69}
   
    local value = module[data[1]](data[2], data[3])
    print("Server", value)

    return data
end
-- client script
local module = require(game.ReplicatedStorage.ModuleScript)

local data = game.ReplicatedStorage.Function:InvokeServer(420)

local value = module[data[1]](data[2], data[3])
print("Client", value)