BindableFunction

Show Deprecated

A BindableFunction lets you bind a function in one script to other scripts on the same side of the client-server boundary. Such functions are invoked through BindableFunction:Invoke() which calls BindableFunction.OnInvoke. Invocations will yield until the corresponding callback is found, and if the callback was never set, the script that invokes it will not resume execution.

As an alternative for one-way communication between two scripts on the same side of the client-server boundary, consider BindableEvent which does not yield for a return.

As stated, BindableFunctions do not allow for communication between the server and clients. If you are looking for this functionality, use a RemoteFunction as outlined in Remote Events and Functions.

Parameter Limitations

Any type of Roblox object such as an Enum, Instance, or others can be passed as a parameter when a BindableFunction is invoked. Luau types such as numbers, strings, and booleans can also be passed, although there are limitations.

Code Samples

BindableFunction Addition

local bindableFunction = script.Parent
-- Define a function for use with the BindableFunction
local function AddTwoNumbers(a, b)
return a + b
end
-- Set the OnInvoke callback to be our function
bindableFunction.OnInvoke = AddTwoNumbers
-- Invoke the BindableFunction
print(bindableFunction:Invoke(25, 44))
BindableFunction Valid Values

local bindableFunction = script.Parent
-- Dummy function
bindableFunction.OnInvoke = function(...)
return ...
end
-- These values CAN be sent to/from BindableFunctions
bindableFunction:Invoke() -- nil
bindableFunction:Invoke(25) -- numbers
bindableFunction:Invoke("hello") -- strings
bindableFunction:Invoke(true) -- booleans
bindableFunction:Invoke("buy", 25) -- multiple values are OK
bindableFunction:Invoke({ 1, 2, 3 }) -- tables as arrays with no gaps
-- note the curly braces
bindableFunction:Invoke({ -- tables with string keys only
hello = "world",
goodbye = "world",
})
bindableFunction:Invoke({ -- tables with string keys
point = { 1, 2 }, -- whose values are also valid
point2 = { 3, 4 },
})
bindableFunction:Invoke({ -- tables as arrays
{ 1, 2, 3 }, -- whose values also are valid
{ hello = "world" },
})
-- These are some values you CANNOT send to/from BindableFunctions
bindableFunction:Invoke({ 1, nil, 3 }) -- tables as arrays cannot have nil gaps
bindableFunction:Invoke({
[{}] = "hello", -- table keys can only be numbers OR strings
})
bindableFunction:Invoke({ -- tables keys cannot be BOTH numbers AND strings
[1] = "apple",
hello = "world",
})

Summary

Properties

Methods

Invoke(arguments: Tuple): Tuple  YIELDS

Calls the BindableFunction.OnInvoke callback of the BindableFunction, returning any values returned by the callback.

Events

Callbacks

OnInvoke(arguments: Tuple)  

Properties

Methods

Invoke

Yields

Calls the BindableFunction.OnInvoke callback and returns any values that were returned by the callback. Invocations will yield until the corresponding callback is found, and if the callback was never set, the script that invokes it will not resume execution.

Any type of Roblox object such as an Enum, Instance, or others can be passed as a parameter when a BindableFunction is invoked. Luau types such as numbers, strings, and booleans can also be passed, although there are limitations. Note that functions referenced as parameters of a passed table will not be replicated, therefore making it impossible to use these objects to pass functions between scripts.

Only one function can be bound to Invoke() at a time. If you assign multiple functions, only the last one assigned will be used.

Parameters

arguments: Tuple

Values to be passed to the callback.


Returns

Values passed to the callback.

Code Samples

BindableFunction Valid Values

local bindableFunction = script.Parent
-- Dummy function
bindableFunction.OnInvoke = function(...)
return ...
end
-- These values CAN be sent to/from BindableFunctions
bindableFunction:Invoke() -- nil
bindableFunction:Invoke(25) -- numbers
bindableFunction:Invoke("hello") -- strings
bindableFunction:Invoke(true) -- booleans
bindableFunction:Invoke("buy", 25) -- multiple values are OK
bindableFunction:Invoke({ 1, 2, 3 }) -- tables as arrays with no gaps
-- note the curly braces
bindableFunction:Invoke({ -- tables with string keys only
hello = "world",
goodbye = "world",
})
bindableFunction:Invoke({ -- tables with string keys
point = { 1, 2 }, -- whose values are also valid
point2 = { 3, 4 },
})
bindableFunction:Invoke({ -- tables as arrays
{ 1, 2, 3 }, -- whose values also are valid
{ hello = "world" },
})
-- These are some values you CANNOT send to/from BindableFunctions
bindableFunction:Invoke({ 1, nil, 3 }) -- tables as arrays cannot have nil gaps
bindableFunction:Invoke({
[{}] = "hello", -- table keys can only be numbers OR strings
})
bindableFunction:Invoke({ -- tables keys cannot be BOTH numbers AND strings
[1] = "apple",
hello = "world",
})
BindableFunction Addition

local bindableFunction = script.Parent
-- Define a function for use with the BindableFunction
local function AddTwoNumbers(a, b)
return a + b
end
-- Set the OnInvoke callback to be our function
bindableFunction.OnInvoke = AddTwoNumbers
-- Invoke the BindableFunction
print(bindableFunction:Invoke(25, 44))

Events

Callbacks

OnInvoke

Parameters

arguments: Tuple

Returns

Code Samples

BindableFunction Addition

local bindableFunction = script.Parent
-- Define a function for use with the BindableFunction
local function AddTwoNumbers(a, b)
return a + b
end
-- Set the OnInvoke callback to be our function
bindableFunction.OnInvoke = AddTwoNumbers
-- Invoke the BindableFunction
print(bindableFunction:Invoke(25, 44))
BindableFunction Valid Values

local bindableFunction = script.Parent
-- Dummy function
bindableFunction.OnInvoke = function(...)
return ...
end
-- These values CAN be sent to/from BindableFunctions
bindableFunction:Invoke() -- nil
bindableFunction:Invoke(25) -- numbers
bindableFunction:Invoke("hello") -- strings
bindableFunction:Invoke(true) -- booleans
bindableFunction:Invoke("buy", 25) -- multiple values are OK
bindableFunction:Invoke({ 1, 2, 3 }) -- tables as arrays with no gaps
-- note the curly braces
bindableFunction:Invoke({ -- tables with string keys only
hello = "world",
goodbye = "world",
})
bindableFunction:Invoke({ -- tables with string keys
point = { 1, 2 }, -- whose values are also valid
point2 = { 3, 4 },
})
bindableFunction:Invoke({ -- tables as arrays
{ 1, 2, 3 }, -- whose values also are valid
{ hello = "world" },
})
-- These are some values you CANNOT send to/from BindableFunctions
bindableFunction:Invoke({ 1, nil, 3 }) -- tables as arrays cannot have nil gaps
bindableFunction:Invoke({
[{}] = "hello", -- table keys can only be numbers OR strings
})
bindableFunction:Invoke({ -- tables keys cannot be BOTH numbers AND strings
[1] = "apple",
hello = "world",
})