Roblox Wiki
Advertisement
Roblox Wiki

Callbacks are members of objects that are called internally by Roblox in certain situations. There are currently only two callbacks in the entire Roblox API. These are the RequestShutdown callback of the DataModel and the OnInvoke callback of the BindableFunction object. There was also previously another callback, called "Receive", which belonged to CustomEvents, but it was removed and was instead replaced by an event.

Icon

The icon of callbacks (File:Callback icon.png) is similar to the icon of methods (Method icon), but you can notice that there is a small difference: they are reversed. Callbacks are the only type of member for which there is no protected icon.

Usage

Callbacks are not very intuitive to use for users who have not used them before, since they are very rarely used.

Callbacks can be used like this:

Object.Callback = func

In the code above, Object is an object, Callback is the name of a callback belonging to the object's class, and func is a function that will be set as the callback. The function can accept parameters that will be given by internal code and can return values to the internal code for various uses.

Example

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', Workspace)
	part.Name = name
	part.Size = size
end

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

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