I'm trying to get a function from a server script to a local script, is this possible? I've tried this
1 | local myRemoteFunction = script.Parent. load -- RemoteFunction |
2 |
3 | function myRemoteFunction.OnServerInvoke(plr,code) |
4 |
5 | return function () print ( "Test" ) end |
6 |
7 | end |
But I don't think return works with functions. I've also tried putting the function in a table, this didn't work either. Thanks. (Someone asked me to put this)
1 | local myRemoteFunction = script.Parent. load -- RemoteFunction |
2 |
3 | function myRemoteFunction.OnServerInvoke(plr,code) |
4 |
5 | return { [ "Func" ] = function () print ( "Test" ) end } |
6 |
7 | end |
No you cannot. See this answer..
It isn't possible to send a function over the network, because functions can change local variables. That means any use of the function would somehow involve reaching into another computer's memory (which is at best dangerous, if not impossible)
Functions are sent across the network for typically several reasons
1) they're a method of a class. In that case, you can just annotate objects with which class they are, and setmetatable
on the receiving end according to the annotate class (see linked answer for example code)
2) you're asking them to invoke a particular function from some set of functions. In that case, just say which function (e.g., by the name). The receiver has to look it up in the table:
1 | -- module RemoteFunctions |
2 | return { |
3 | printTest = function () |
4 | print ( "test" ) |
5 | end , |
6 | } |
1 | -- client |
2 | remote:InvokeServer( "printTest" ) |
01 | local Fs = require(RemoteFunctionsModuleScript) |
02 |
03 | -- server |
04 | function remote.OnServerInvoke(player, fun) |
05 | if not Fs [ fun ] then |
06 | return "invalid function name" |
07 | end |
08 |
09 | return Fs [ fun ] () |
10 | end |
Have you tried loadstrings? when you want to run a local in a server script try
1 | lp = game.Players.<UsernameHere> |
2 | lp:runLocalScript [[( |
3 | lp=game.Players.LocalPlayer |
4 | == |
5 | LocalCode Here |
6 | == |
7 | )]] |