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.


-- Mixed table
local colorData = {}
colorData[1] = "Blue"
colorData[2] = "Yellow"
colorData["Color1"] = "Green"
colorData["Color2"] = "Red"
-- Table with two key-indexed sub-tables
local playerData = {}
playerData["CharData"] = {
-- All children indexed by key
CharName = "Diva Dragonslayer",
CharClass = "Knight"
}
playerData["Inventory"] = {
-- All children numerically indexed
"Sword",
"Bow",
"Rope"
}

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

local bindableEvent = script.Parent
-- Print numbers 1 through `n`
local function count(n)
for i = 1, n do
task.wait(0.25)
print(i)
end
end
-- Print the first `n` letters of the alphabet
local function alphabet(n)
for i = 0, n - 1 do
task.wait(0.333)
print(string.char(65 + i))
end
end
-- This function errors! But it doesn't stop
-- the others from being called
local function ohNo(n)
if n > 10 then
error("Oh no!")
end
end
bindableEvent.Event:Connect(count)
bindableEvent.Event:Connect(alphabet)
bindableEvent.Event:Connect(ohNo)
bindableEvent:Fire(26)
print("Let's-a-go!")
BindableEvent Valid Values

local bindableEvent = script.Parent
-- Define a simple function to connect
-- to the custom event
local function onEvent(...)
print(...)
end
bindableEvent.Event:Connect(onEvent)
-- These values CAN be sent when firing BindableEvents
bindableEvent:Fire() -- nil
bindableEvent:Fire(25) -- numbers
bindableEvent:Fire("hello") -- strings
bindableEvent:Fire(true) -- booleans
bindableEvent:Fire("buy", 25) -- multiple values are OK
bindableEvent:Fire({ 1, 2, 3 }) -- tables as arrays with no gaps
-- note the curly braces
bindableEvent:Fire({ -- tables with string keys only
hello = "world",
goodbye = "world",
})
bindableEvent:Fire({ -- tables with string keys
point = { 1, 2 }, -- whose values are also valid
point2 = { 3, 4 },
})
bindableEvent:Fire({ -- tables as arrays
{ 1, 2, 3 }, -- whose values also are valid
{ hello = "world" },
})
-- These are some values you CANNOT send to/from BindableFunctions
bindableEvent:Fire({ 1, nil, 3 }) -- tables as arrays cannot have nil gaps
bindableEvent:Fire({
[{}] = "hello", -- table keys can only be numbers OR strings
})
bindableEvent:Fire({ -- tables keys cannot be BOTH numbers AND strings
[1] = "apple",
hello = "world",
})
BindableEvent Hello World

local bindableEvent = script.Parent
-- Define a simple function to connect to the custom event
local function onEvent(...)
print(...)
end
bindableEvent.Event:Connect(onEvent)
-- Fire the event
bindableEvent:Fire("Hello, world")

Summary

Properties

Methods

Fire(arguments: Tuple): void  

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

Events

Event(arguments: Tuple): RBXScriptSignal  

The event that is fired when Fire is called.

Properties

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.


-- Mixed table
local colorData = {}
colorData[1] = "Blue"
colorData[2] = "Yellow"
colorData["Color1"] = "Green"
colorData["Color2"] = "Red"
-- Table with two key-indexed sub-tables
local playerData = {}
playerData["CharData"] = {
-- All children indexed by key
CharName = "Diva Dragonslayer",
CharClass = "Knight"
}
playerData["Inventory"] = {
-- All children numerically indexed
"Sword",
"Bow",
"Rope"
}
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

local bindableEvent = script.Parent
-- Print numbers 1 through `n`
local function count(n)
for i = 1, n do
task.wait(0.25)
print(i)
end
end
-- Print the first `n` letters of the alphabet
local function alphabet(n)
for i = 0, n - 1 do
task.wait(0.333)
print(string.char(65 + i))
end
end
-- This function errors! But it doesn't stop
-- the others from being called
local function ohNo(n)
if n > 10 then
error("Oh no!")
end
end
bindableEvent.Event:Connect(count)
bindableEvent.Event:Connect(alphabet)
bindableEvent.Event:Connect(ohNo)
bindableEvent:Fire(26)
print("Let's-a-go!")
BindableEvent Valid Values

local bindableEvent = script.Parent
-- Define a simple function to connect
-- to the custom event
local function onEvent(...)
print(...)
end
bindableEvent.Event:Connect(onEvent)
-- These values CAN be sent when firing BindableEvents
bindableEvent:Fire() -- nil
bindableEvent:Fire(25) -- numbers
bindableEvent:Fire("hello") -- strings
bindableEvent:Fire(true) -- booleans
bindableEvent:Fire("buy", 25) -- multiple values are OK
bindableEvent:Fire({ 1, 2, 3 }) -- tables as arrays with no gaps
-- note the curly braces
bindableEvent:Fire({ -- tables with string keys only
hello = "world",
goodbye = "world",
})
bindableEvent:Fire({ -- tables with string keys
point = { 1, 2 }, -- whose values are also valid
point2 = { 3, 4 },
})
bindableEvent:Fire({ -- tables as arrays
{ 1, 2, 3 }, -- whose values also are valid
{ hello = "world" },
})
-- These are some values you CANNOT send to/from BindableFunctions
bindableEvent:Fire({ 1, nil, 3 }) -- tables as arrays cannot have nil gaps
bindableEvent:Fire({
[{}] = "hello", -- table keys can only be numbers OR strings
})
bindableEvent:Fire({ -- tables keys cannot be BOTH numbers AND strings
[1] = "apple",
hello = "world",
})

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.


-- Mixed table
local colorData = {}
colorData[1] = "Blue"
colorData[2] = "Yellow"
colorData["Color1"] = "Green"
colorData["Color2"] = "Red"
-- Table with two key-indexed sub-tables
local playerData = {}
playerData["CharData"] = {
-- All children indexed by key
CharName = "Diva Dragonslayer",
CharClass = "Knight"
}
playerData["Inventory"] = {
-- All children numerically indexed
"Sword",
"Bow",
"Rope"
}
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

local bindableEvent = script.Parent
-- Define a simple function to connect to the custom event
local function onEvent(...)
print(...)
end
bindableEvent.Event:Connect(onEvent)
-- Fire the event
bindableEvent:Fire("Hello, world")