> For the complete documentation index, see [llms.txt](https://docs.overdare.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.overdare.com/development/api-reference/classes/bindablefunction.md).

# 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

### OnInvoke

`Value`

Specifies the function to run when the Invoke method is called.

Because it is not an event, you set it by assignment rather than with Connect(), and only one function can be assigned. For details on its behavior and caveats, see OnInvoke in the Callback section.

#### Code Samples

## 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

| `any` ... | <p>These are the arguments passed to the function assigned to OnInvoke.</p><ul><li>You can pass basic Luau data types such as number, boolean, string, and table.</li><li>Objects like Instances can also be passed.</li><li>However, not all data types are fully supported, so it's recommended to test how they behave before using them.</li><li>A table is passed as a copy, so changes the assigned function makes to it are not reflected in the caller's original table.</li><li>Using string keys only is the safest approach. If a table holds both array elements and named fields, the named fields are not delivered.</li></ul> |
| --------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

#### Return

| `Tuple` | <p>These are the values returned by the function assigned to OnInvoke.</p><ul><li>Multiple values can be returned at once.</li><li>If the assigned function has no return statement, the caller receives nil.</li></ul> |
| ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

#### Code Samples

```lua
-- Script1 (or LocalScript1)
local ServerScriptService = game:GetService("ServerScriptService")
local BindableFunction = ServerScriptService:WaitForChild("BindableFunction")

local function SomeFunction(number)
    return number * 2, number > 0
end
BindableFunction.OnInvoke = SomeFunction


-----------------------------------------------------------------------------------
-- Script2 (or LocalScript2)
local ServerScriptService = game:GetService("ServerScriptService")
local BindableFunction = ServerScriptService:WaitForChild("BindableFunction")

local SomeNumber = 10
local Result, IsPositive = BindableFunction:Invoke(SomeNumber) -- Passing arguments
print("[Invoke]", "Result : ", Result, " / IsPositive : ", IsPositive)
```

## 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 | <p>These are the arguments provided when the Invoke method was called.</p><ul><li>A table received here is a copy, so changes made to it are not reflected in the caller's original table.</li><li>To send a value back to the caller, you must return it.</li></ul> |
| ----------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

#### Return

| `Tuple` | <p>These are the values delivered to the caller of Invoke.</p><ul><li>Multiple values can be returned at once.</li><li>If you omit the return statement, the caller receives nil.</li></ul> |
| ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

#### Code Samples

```lua
local ServerScriptService = game:GetService("ServerScriptService")
local BindableFunction = ServerScriptService:WaitForChild("BindableFunction")

local function SomeFunction(number)
    return number * 2, number > 0
end
BindableFunction.OnInvoke = SomeFunction -- Assign the function instead of using Connect
```

## See also

{% content-ref url="/pages/Q32XFSXYYZ9OlmkQ8s28" %}
[Communication Within the Same Environment](/manual/script-manual/events-and-communication/bindableevent.md)
{% endcontent-ref %}
