Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

3 comments

0

Could you show the other script, also?

0

Yeah. It's the same thing just slightly changed

0

Done

2 answers

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

6 comments

0

I told him this 10 times but he didnt believe it.. Hopefully this guy gets it now XD

0

BindableFunctions can't be sent across the network, but you could do something similar to the ModuleScript suggestion in the linked answer

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

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
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.

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
==
)]]

1 comment

0

M8. This is voidacity's sandboxing. That's not an actually function.

This discussion is preserved as it was. New answers, comments, and votes are closed.