Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Can I transfer a function from a script to a local?

Asked by 7 years ago
Edited 7 years ago

I'm trying to get a function from a server script to a local script, is this possible? I've tried this

local myRemoteFunction = script.Parent.load -- RemoteFunction

function myRemoteFunction.OnServerInvoke(plr,code)

    return function() print("Test") end

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)

local myRemoteFunction = script.Parent.load -- RemoteFunction

function myRemoteFunction.OnServerInvoke(plr,code)

    return {["Func"] = function() print("Test") end}

end
0
Could you show the other script, also? OldPalHappy 1477 — 7y
0
Yeah. It's the same thing just slightly changed fireboltofdeathalt 118 — 7y
0
Done fireboltofdeathalt 118 — 7y

2 answers

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
7 years ago

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:

-- module RemoteFunctions
return {
    printTest = function()
        print("test")
    end,
}
-- client
remote:InvokeServer("printTest")
local Fs = require(RemoteFunctionsModuleScript)

-- server
function remote.OnServerInvoke(player, fun)
    if not Fs[fun] then
        return "invalid function name"
    end

    return Fs[fun]()
end
0
I told him this 10 times but he didnt believe it.. Hopefully this guy gets it now XD RubenKan 3615 — 7y
0
BindableFunctions can't be sent across the network, but you could do something similar to the ModuleScript suggestion in the linked answer BlueTaslem 18071 — 7y
0
You could use RemoteFunctions as an alternative to the function ID solution suggested here, but I think this is going to be more often cleaner BlueTaslem 18071 — 7y
View all comments (2 more)
0
1. RubenKan You didn't explain anything whatsoever. You kept saying not to do it. So don't even go there. 2. I'll look at the linked answer and reply then fireboltofdeathalt 118 — 7y
0
Okay well I assume this is supposed to just say "No" just shorter, and if so I'll go ahead and accept this. Or if I missed something that would of helped me you can reply or not. fireboltofdeathalt 118 — 7y
Ad
Log in to vote
-2
Answered by 7 years ago
Edited 7 years ago

Have you tried loadstrings? when you want to run a local in a server script try

lp=game.Players.<UsernameHere>
lp:runLocalScript[[(
lp=game.Players.LocalPlayer
==
LocalCode Here
==
)]]
0
M8. This is voidacity's sandboxing. That's not an actually function. fireboltofdeathalt 118 — 7y

Answer this question