You can bind code to custom behavior and communicate between scripts using bindable events and functions. BindableEvent objects allow asynchronous, one-way communication, and BindableFunction objects allow synchronous, two-way communication.
Bindable events and functions can't communicate between scripts across the client-server boundary. For example, if you create a BindableEvent in ReplicatedStorage and connect a function to it from a LocalScript, then firing that BindableEvent from a Script doesn't call the function. To communicate between scripts across the client-server boundary, use Remote Events and Functions.
BindableEvents
BindableEvent objects allow asynchronous, one-way communication between scripts. They contain an Event that you can fire manually by calling its Fire method without yielding for return values. The connected function receives arguments that you pass to Fire.
Creating BindableEvents
To create a new BindableEvent in the Explorer:
- Hover over the container in the Explorer into which you want to insert a BindableEvent. It's common to put BindableEvents in an Event folder in ServerScriptService to use them with Scripts and ReplicatedStorage to use them with LocalScripts.
- Click the ⊕ button that appears to the right of the container to open the Insert Object menu.
- Select BindableEvent.
- Rename the event to describe its purpose using PascalCase.
To create a new BindableEvent in a Script, use Instance.new():
-- Script in ServerScriptServicelocal roundStartedEvent = Instance.new("BindableEvent")roundStartedEvent.Name = RoundStartedroundStartedEvent.Parent = ServerScriptService
Using BindableEvents
You can connect multiple functions to the same BindableEvent, but Luau executes them in an unpredictable order. To ensure that functions execute in a particular order, combine their bodies or calls into a single function to connect to the event.
To fire a BindableEvent in ServerScriptService named ShareData:
-- Script in ServerScriptService named DataSenderlocal ServerScriptService = game:GetService("ServerScriptService")local shareData = ServerScriptService.ShareDatalocal HELLO_WORLD = "Hello world!"-- Shares HELLO_WORLD after 2 secondswait(2)shareData:Fire(HELLO_WORLD)
To connect to a BindableEvent in ServerScriptService named ShareData:
-- Script in ServerScriptService named DataReceiver
local ServerScriptService = game:GetService("ServerScriptService")
local shareData = ServerScriptService.ShareData
shareData.Event:Connect(function(data)
print(data)
end)
BindableFunctions
BindableFunction objects allow for synchronous, two-way communication between scripts. They contain an OnInvoke callback that you can define in one script and call from other scripts. The callback function runs when you call the Invoke() method on the BindableFunction. The callback function arguments that you pass to Invoke(). The code invoking the function yields until the function halts or returns a value.
Creating BindableFunctions
To create a new BindableFunction in the Explorer:
- Hover over the container in the Explorer into which you want to insert a BindableFunction. It's common to put BindableFunction objects in a Functions folder in ServerScriptService to use them with Scripts and ReplicatedStorage to use them with LocalScripts.
- Click the ⊕ button that appears to the right of the container to open the Insert Object menu.
- Select BindableFunction.
- Rename the function to describe its purpose using PascalCase.
To create a new BindableFunction in a Script:
-- Script in ServerScriptServicelocal getHelloWorld = Instance.new("BindableFunction")getHelloWorld.Name = GetHelloWorldgetHelloWorld.Parent = ServerScriptService
Using BindableFunctions
Each BindableFunction has only one OnInvoke callback. If you have multiple definitions, then only the one defined latest runs. If the OnInvoke callback does not have a return statement, it returns nil.
To define the OnInvoke callback of a BindableFunction in ServerScriptService named GetHelloWorld:
-- Script in ServerScriptService named DataManager
local ServerScriptService = game:GetService("ServerScriptService")
local getHelloWorld = ServerScriptService.GetHelloWorld
local HELLO_WORLD = "Hello world!"
getHelloWorld.OnInvoke = function()
return HELLO_WORLD
end
To invoke a BindableFunction in ServerScriptService named GetHelloWorld:
-- Script in ServerScriptService named DataReceiverlocal ServerScriptService = game:GetService("ServerScriptService")local getHelloWorld = ServerScriptService.GetHelloWorldprint(getHelloWorld:Invoke())print("Hello world again!")
Notice that the "Hello world!" prints before "Hello world again!" because the Script yields until the BindableFunction halts or returns a value.
Parameter Limitations
Any type of Roblox object such as an Enum, Instance, or others can be passed as a parameter when a BindableEvent is fired or a BindableFunction is invoked. Luau types such as numbers, strings, and booleans can also be passed, although there are limitations.