Callback (Tutorial)

From Legacy Roblox Wiki
Jump to navigationJump to search

Callbacks are members of objects that are called internally by ROBLOX in certain situations. There are currently only two callbacks in the whole 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 () is similar to the icon of methods (), but you can notice that there is a little difference.

Usage

Callbacks are not usually intuitive to use, especially since they are extremly rarely used.

You could consider them as a mix of events and properties.

Here is how you use a callback:

Object.Callback = function

In the code above, Object is an object, Callback is the name of a callback belonging to the object, and function 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))