On the Developer Hub on the BindableFunctions page, it notes how:
Functions put in BindableFunctions will not be replicated, therefore making it impossible to use these objects to pass functions between scripts.
To me, this means that I cannot have a function return a function to a BindableFunction, as shown:
Listener
:
local bindableFunction = script.Parent.BindableFunction
local function helloWorld()
print("hello world")
end
local function onInvoke()
local itemsToReturn = {}
itemsToReturn.Function = helloWorld
return itemsToReturn
end
bindableFunction.OnInvoke = onInvoke
Invoker
:
local bindableFunction = script.Parent.BindableFunction
local result = bindableFunction:Invoke()
result.Function()
In short, Invoker
invokes the BindableFunction to which Listener
is listening for and executes onInvoke, which returns the function helloWorld
. Based on the Developer Hub, the final line in Invoker
should error, either because result.Function
is nil
(or some other related error).
However, based on my testing, the line above erroring does not happen, and the function executes successfully:
As can be seen in the bottom image, hello world
was successfully ran without error when called by Invoker
.
Is this intended, and either I’m misinterpreting the Developer Hub’s article or the Developer Hub’s article is out of date; or, is this a bug that I shouldn’t make use of?
I ask this because I’m currently in the process of making my own command system that makes use of this: one script loads all command scripts in a given folder, then listens to a BindableFunction for invocations, returning the command’s function; then a different script actually invokes the function returned by the previous script.
(Here’s a diagram):
(Sidenote, that command system will probably never appear here…because it’s probably full of spaghetti code based on my coding skills, and I’m fairly sure nearly any comptent scripter reading this could make a much better system than mine.)