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

Why can't I return a function from a RemoteFunction?

In a script inside a RemoteFunction, which is inside workspace, I have the following:

function script.Parent.OnServerInvoke(_)
    return function()
		print("hi");
	end;
end;

Then, inside a LocalScript, which is inside the StarterGui, I have the following:

local function loadstring(src)
	return workspace.RemoteFunction:InvokeServer(src);
end;
loadstring()();

This causes the following error:

12:35:23.263 - Players.Player1.PlayerGui.LocalScript:4: attempt to call a nil value
12:35:23.263 - Stack Begin
12:35:23.263 - Script 'Players.Player1.PlayerGui.LocalScript', Line 4
12:35:23.263 - Stack End

What's wrong with this application?

1 comment

0

Probably to prevent that loadstring hack where you can just return a function from the server. But there might be an actual reason.

1 answer

As I'm not quite positive why this is happening I will give two posibilities.

1: The return function is actually returning the return value of the function due to some strange evaluation priority. 2(more likely). I'm fairly sure that a function when defined takes a snapshot of the variable environment which is certainly a security concern if you're moving this function to the client. Why ROBLOX made the return value nil rather then just erroring when a function is returned I don't know.

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