Callback

Callbacks are write-only members of objects that are set to functions. You cannot invoke a callback directly — Roblox will internally trigger the callback function.

Notable Callbacks[edit]

Usage[edit]

To set a callback, you assign it to a function:

local bindableFunction = Instance.new("BindableFunction")
bindableFunction.OnInvoke = function()
	return 1337
end

Callbacks are write-only, meaning you cannot get the value of a callback.

local bindableFunction = Instance.new("BindableFunction")
print(bindableFunction.OnInvoke)
--> OnInvoke is a callback member of BindableFunction; you can only set the callback value, get is not available

To invoke the callback, in this case you'll use BindableFunction:Invoke(). All of the arguments you pass to :Invoke() will be passed to the callback function, and then the return value from the callback function will be returned to the caller of :Invoke().

local bindableFunction = Instance.new("BindableFunction")
bindableFunction.OnInvoke = function(n)
	return 2 * n
end
 
print(bindableFunction:Invoke(42))
--> 84

Icon[edit]

The icon of callbacks (Callback icon.png) is similar to the icon of methods (Function Icon.png), but you can notice that there is a little difference.

Example[edit]

Let's imagine a simple example: you want to make a BindableFunction and bind it to a function that receives two arguments, let's say, the Name of a part and its Size, and that creates a Part, puts it in the Workspace, and give it the Name and the Size passed to the function.

You would first create the BindableFunction:

local func = Instance.new('BindableFunction', script.Parent)

And you would then connect it to your function, using the OnInvoke callback:

local func = Instance.new('BindableFunction', script.Parent)
func.OnInvoke = function(name, size)
	local part = Instance.new('Part')
	part.Name = name
	part.Size = size
	part.Parent = workspace
end

Now, you can just call the BindableFunction like this, and it will create the part:

BindableFunction:Invoke("Brick", Vector3.new(50, 50, 50))