BindableEvent

Show Deprecated

An object that allows events defined in one server-side Script to be subscribed to by another script for one-way communication.

BindableEvents do not allow for communication between the server and client. If you are looking for this functionality use RemoteEvent.

BindableEvents vs BindableFunctions

Unlike BindableEvents, BindableFunctions allow for two-way communication between two scripts:

  • When a script fires a BindableEvent it does not yield for a return. The script continues executing as the event is handled by the subscribed script asynchronously.
  • When a script invokes a BindableFunction, however, that script yields until the event is handled and returned by the subscribed script synchronously. This allows for scripts to effectively pass data during and after an event.

BindableEvents vs RemoteEvents

While BindableEvents allow for one-way communication server-server scripts or client-client LocalScripts, RemoteEvents allow for one-way communicate the server and client. For more information on RemoteEvents, see Remote Events and Functions.

Parameter Limitations

Any type of Roblox object such as an Enum, Instance, or userdata can be passed as a parameter when a RemoteEvent is fired or a RemoteFunction invoked. Lua types such as numbers, strings, and booleans can also be passed, although there are some limitations on how data can be passed.

Mixed Tables

If a Table is passed as an argument to a BindableEvent it must be an array without missing entries or have string keys, not a mixture, or else the string keys will be lost.

Avoid passing a mixed table (some values indexed by number and others by key), as only the data indexed by number will be passed. For example, when the server receives the colorData table illustrated below, it only sees indices 1 and 2 containing "Blue" and "Yellow" while the other data is lost in the transfer. Note, however, that sub-tables do not need to be indexed in the same way as their parent — in other words, as long as each individual sub-table is indexed with the same type, all of the data is preserved.

Metatables are not preserved.

Non-String Indices

If any indices of a passed table are non-string type (Instance, userdata, function, another table, etc.), those indices will be converted to a string.


1-- Mixed table
2local colorData = {}
3colorData[1] = "Blue"
4colorData[2] = "Yellow"
5colorData["Color1"] = "Green"
6colorData["Color2"] = "Red"
7
8-- Table with two key-indexed sub-tables
9local playerData = {}
10playerData["CharData"] = {
11 -- All children indexed by key
12 CharName = "Diva Dragonslayer",
13 CharClass = "Knight"
14}
15playerData["Inventory"] = {
16 -- All children numerically indexed
17 "Sword",
18 "Bow",
19 "Rope"
20}
21

Functions

Functions passed as parameters will not be replicated, therefore making it impossible to use these objects to pass functions between scripts.

Code Samples

BindableEvent Threading Behavior

1local bindableEvent = script.Parent
2
3-- Print numbers 1 through `n`
4local function count(n)
5 for i = 1, n do
6 task.wait(0.25)
7 print(i)
8 end
9end
10
11-- Print the first `n` letters of the alphabet
12local function alphabet(n)
13 for i = 0, n - 1 do
14 task.wait(0.333)
15 print(string.char(65 + i))
16 end
17end
18
19-- This function errors! But it doesn't stop
20-- the others from being called
21local function ohNo(n)
22 if n > 10 then
23 error("Oh no!")
24 end
25end
26
27bindableEvent.Event:Connect(count)
28bindableEvent.Event:Connect(alphabet)
29bindableEvent.Event:Connect(ohNo)
30bindableEvent:Fire(26)
31print("Let's-a-go!")
BindableEvent Valid Values

1local bindableEvent = script.Parent
2
3-- Define a simple function to connect
4-- to the custom event
5local function onEvent(...)
6 print(...)
7end
8bindableEvent.Event:Connect(onEvent)
9
10-- These values CAN be sent when firing BindableEvents
11bindableEvent:Fire() -- nil
12bindableEvent:Fire(25) -- numbers
13bindableEvent:Fire("hello") -- strings
14bindableEvent:Fire(true) -- booleans
15bindableEvent:Fire("buy", 25) -- multiple values are OK
16bindableEvent:Fire({ 1, 2, 3 }) -- tables as arrays with no gaps
17-- note the curly braces
18bindableEvent:Fire({ -- tables with string keys only
19 hello = "world",
20 goodbye = "world",
21})
22bindableEvent:Fire({ -- tables with string keys
23 point = { 1, 2 }, -- whose values are also valid
24 point2 = { 3, 4 },
25})
26bindableEvent:Fire({ -- tables as arrays
27 { 1, 2, 3 }, -- whose values also are valid
28 { hello = "world" },
29})
30
31-- These are some values you CANNOT send to/from BindableFunctions
32bindableEvent:Fire({ 1, nil, 3 }) -- tables as arrays cannot have nil gaps
33bindableEvent:Fire({
34 [{}] = "hello", -- table keys can only be numbers OR strings
35})
36bindableEvent:Fire({ -- tables keys cannot be BOTH numbers AND strings
37 [1] = "apple",
38 hello = "world",
39})
BindableEvent Hello World

1local bindableEvent = script.Parent
2
3-- Define a simple function to connect to the custom event
4local function onEvent(...)
5 print(...)
6end
7bindableEvent.Event:Connect(onEvent)
8
9-- Fire the event
10bindableEvent:Fire("Hello, world")

Summary

Properties

Events

Event(arguments: Tuple): RBXScriptSignal  

The event that is fired when Fire is called.

Methods

Fire(arguments: Tuple): void  

Fires the BindableEvent, which fires the BindableEvent.Event event.

Properties

Events

Event

This event is fired when any script calls the BindableEvent:Fire() method is called, using the same arguments as parameters.

Parameter Limitations

Any type of Roblox object such as an Enum, Instance, or userdata can be passed as a parameter when a RemoteEvent is fired or a RemoteFunction invoked. Lua types such as numbers, strings, and booleans can also be passed, although there are some limitations on how data can be passed.

Mixed Tables

If a Table is passed as an argument to a BindableEvent it must be an array without missing entries or have string keys, not a mixture, or else the string keys will be lost.

Avoid passing a mixed table (some values indexed by number and others by key), as only the data indexed by number will be passed. For example, when the server receives the colorData table illustrated below, it only sees indices 1 and 2 containing "Blue" and "Yellow" while the other data is lost in the transfer. Note, however, that sub-tables do not need to be indexed in the same way as their parent — in other words, as long as each individual sub-table is indexed with the same type, all of the data is preserved.

Metatables are not preserved.

Non-String Indices

If any indices of a passed table are non-string type (Instance, userdata, function, another table, etc.), those indices will be converted to a string.


1-- Mixed table
2local colorData = {}
3colorData[1] = "Blue"
4colorData[2] = "Yellow"
5colorData["Color1"] = "Green"
6colorData["Color2"] = "Red"
7
8-- Table with two key-indexed sub-tables
9local playerData = {}
10playerData["CharData"] = {
11 -- All children indexed by key
12 CharName = "Diva Dragonslayer",
13 CharClass = "Knight"
14}
15playerData["Inventory"] = {
16 -- All children numerically indexed
17 "Sword",
18 "Bow",
19 "Rope"
20}
21
Functions

Functions passed as parameters will not be replicated, therefore making it impossible to use these objects to pass functions between scripts.

Parameters

arguments: Tuple

The arguments provided when the event is fired.


Code Samples

BindableEvent Hello World

1local bindableEvent = script.Parent
2
3-- Define a simple function to connect to the custom event
4local function onEvent(...)
5 print(...)
6end
7bindableEvent.Event:Connect(onEvent)
8
9-- Fire the event
10bindableEvent:Fire("Hello, world")

Methods

Fire

void

Calling this method will fire the BindableEvent.Event event.

This function does not yield, even if no script has connected to the "Event" event and even if a connected function yields.

Parameter Limitations

Any type of Roblox object such as an Enum, Instance, or userdata can be passed as a parameter when a RemoteEvent is fired or a RemoteFunction invoked. Lua types such as numbers, strings, and booleans can also be passed, although there are some limitations on how data can be passed.

Mixed Tables

If a Table is passed as an argument to a BindableEvent it must be an array without missing entries or have string keys, not a mixture, or else the string keys will be lost.

Avoid passing a mixed table (some values indexed by number and others by key), as only the data indexed by number will be passed. For example, when the server receives the colorData table illustrated below, it only sees indices 1 and 2 containing "Blue" and "Yellow" while the other data is lost in the transfer. Note, however, that sub-tables do not need to be indexed in the same way as their parent — in other words, as long as each individual sub-table is indexed with the same type, all of the data is preserved.

Metatables are not preserved.

Non-String Indices

If any indices of a passed table are non-string type (Instance, userdata, function, another table, etc.), those indices will be converted to a string.


1-- Mixed table
2local colorData = {}
3colorData[1] = "Blue"
4colorData[2] = "Yellow"
5colorData["Color1"] = "Green"
6colorData["Color2"] = "Red"
7
8-- Table with two key-indexed sub-tables
9local playerData = {}
10playerData["CharData"] = {
11 -- All children indexed by key
12 CharName = "Diva Dragonslayer",
13 CharClass = "Knight"
14}
15playerData["Inventory"] = {
16 -- All children numerically indexed
17 "Sword",
18 "Bow",
19 "Rope"
20}
21
Functions

Functions passed as parameters will not be replicated, therefore making it impossible to use these objects to pass functions between scripts.

Parameters

arguments: Tuple

Returns

void

Code Samples

BindableEvent Threading Behavior

1local bindableEvent = script.Parent
2
3-- Print numbers 1 through `n`
4local function count(n)
5 for i = 1, n do
6 task.wait(0.25)
7 print(i)
8 end
9end
10
11-- Print the first `n` letters of the alphabet
12local function alphabet(n)
13 for i = 0, n - 1 do
14 task.wait(0.333)
15 print(string.char(65 + i))
16 end
17end
18
19-- This function errors! But it doesn't stop
20-- the others from being called
21local function ohNo(n)
22 if n > 10 then
23 error("Oh no!")
24 end
25end
26
27bindableEvent.Event:Connect(count)
28bindableEvent.Event:Connect(alphabet)
29bindableEvent.Event:Connect(ohNo)
30bindableEvent:Fire(26)
31print("Let's-a-go!")
BindableEvent Valid Values

1local bindableEvent = script.Parent
2
3-- Define a simple function to connect
4-- to the custom event
5local function onEvent(...)
6 print(...)
7end
8bindableEvent.Event:Connect(onEvent)
9
10-- These values CAN be sent when firing BindableEvents
11bindableEvent:Fire() -- nil
12bindableEvent:Fire(25) -- numbers
13bindableEvent:Fire("hello") -- strings
14bindableEvent:Fire(true) -- booleans
15bindableEvent:Fire("buy", 25) -- multiple values are OK
16bindableEvent:Fire({ 1, 2, 3 }) -- tables as arrays with no gaps
17-- note the curly braces
18bindableEvent:Fire({ -- tables with string keys only
19 hello = "world",
20 goodbye = "world",
21})
22bindableEvent:Fire({ -- tables with string keys
23 point = { 1, 2 }, -- whose values are also valid
24 point2 = { 3, 4 },
25})
26bindableEvent:Fire({ -- tables as arrays
27 { 1, 2, 3 }, -- whose values also are valid
28 { hello = "world" },
29})
30
31-- These are some values you CANNOT send to/from BindableFunctions
32bindableEvent:Fire({ 1, nil, 3 }) -- tables as arrays cannot have nil gaps
33bindableEvent:Fire({
34 [{}] = "hello", -- table keys can only be numbers OR strings
35})
36bindableEvent:Fire({ -- tables keys cannot be BOTH numbers AND strings
37 [1] = "apple",
38 hello = "world",
39})