What is the difference between BindableEvents and BindableFunctions?

I’ve been looking around a bit and I really can’t seem to find a difference between BindableEvents and BindableFunctions. From what I’ve seen BindableEvents do anything a BindableFunction can do.

So, whats the difference?

BindableFunctions return a value, BindableEvents do not.

-- BindableEvent

-- This is an event connection, so it will run once fired.
BindableEvent.Event:Connect(function(arg)
    print(arg) --> prints "Hello World!"
end)

BindableEvent:Fire("Hello World!")
---------------------------------------------------

-- BindableFunction

-- This will run and return a value once invoked
BindableFunction.OnInvoke = function(value)
   return value + 5
end

print(BindableFunction:Invoke(12)) --> prints 17
2 Likes

There are a couple differences between them.

Return values

BindableFunctions can return values back to the place of invocation. BindableEvents on the other hand are fire-and-forget.

Yielding

BindableEvents do not yield, whereas BindableFunctions do. Let’s look at the following example:

-- SCRIPT A
local t = tick()
BindableFunctionA:Invoke()
print(tick() - t) -- this will print roughly 1.00000


-- SCRIPT B
BindableFunctionA.OnInvoke = function()
  wait(1) -- this causes the other script to yield for a second
  return
end

Here, the code ran in script A will pause for a second because it is waiting on the BindableFunction to return.

Connections

BindableFunctions can only be registered once, meaning the following code is invalid:

BindableFunctionA.OnInvoke = function()
  return true
end

BindableFunctionA.OnInvoke = function() -- this is invalid as a function has already been assigned
  return false
end

BindableEvents on the other hand can be connected multiple times.

BindableEventA.Event:Connect(
  function()
    print("this works!")
  end
)

BindableEventA.Event:Connect(
  function()
    print("this also works!")
  end
)

There are some subtle differences on the implementation level as well that aren’t super worth mentioning, such as how BindableEvents are deferred nowadays but BindableFunctions aren’t. But the 3 things mentioned above are the biggest differences to be aware of.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.