BindableFunction
BindableFunction : Instance
Overview
A BindableFunction is an Object that enables synchronous, two-way communication between scripts of the same kind, either between client-side scripts or between server-side scripts.
The script that calls Invoke yields until the function assigned to OnInvoke returns, and it can receive multiple return values at once.
If the function assigned to OnInvoke throws an error, that error propagates to the caller of Invoke.
If communication between the client and server is needed, use RemoteFunction instead.
If you don't need a return value, BindableEvent is a better fit, since Invoke suspends the calling script.
Properties
Methods
Invoke
Runs the function assigned to the BindableFunction's OnInvoke and returns its return values as-is.
If no function is assigned to OnInvoke, the script that called this method never resumes, and no error or timeout occurs. Make sure your initialization order assigns OnInvoke before any call is made.
If the function assigned to OnInvoke throws an error, that error propagates to the caller, so you can handle it by wrapping the call in pcall().
When several calls are made to the same BindableFunction at the same time, the assigned function runs for each of them.
Parameters
Tuple Arguments
These are the arguments passed to the function assigned to OnInvoke.
You can pass basic Luau data types such as number, boolean, string, and table.
Objects like Instances can also be passed.
However, not all data types are fully supported, so it's recommended to test how they behave before using them.
A table is passed as a copy, so changes the assigned function makes to it are not reflected in the caller's original table.
Using string keys only is the safest approach. If a table holds both array elements and named fields, the named fields are not delivered.
Return
Tuple
These are the values returned by the function assigned to OnInvoke.
Multiple values can be returned at once.
If the assigned function has no return statement, the caller receives nil.
Code Samples
Events
Callback
OnInvoke
When the Invoke method is called on a BindableFunction instance, the function assigned to OnInvoke is run, and the arguments provided during the call are passed along as-is.
OnInvoke is not an event but a property that holds a function, so it cannot be used with Connect(). Assign the function to it instead.
Important Notes
Only one function can be assigned. If you assign more than once, only the last assigned function runs.
Assigning a value that is not a function does not raise an error at assignment time. The error occurs when Invoke is called.
If you replace OnInvoke while the assigned function is running, calls already in progress finish with the previous function, and only new calls made afterward use the new one.
If the assigned function yields for a long time, the caller of Invoke waits just as long.
Parameters
Tuple Arguments
These are the arguments provided when the Invoke method was called.
A table received here is a copy, so changes made to it are not reflected in the caller's original table.
To send a value back to the caller, you must return it.
Return
Tuple
These are the values delivered to the caller of Invoke.
Multiple values can be returned at once.
If you omit the return statement, the caller receives nil.
Code Samples
See also
Communication Within the Same EnvironmentLast updated