Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What is Bindable Function and Bindable Event?

Asked by 4 years ago
Edited 4 years ago

So i saw both of theese and i don't know about it and what they are used and their differences is.

0
did you mean bindable event and bindable function? SoftlockedUnderZero 668 — 4y
0
Bindable Events help on communication between server scripts. Unlike the Remote event that only allows client to server communication. Bindable Events cannot make local scripts and server scripts communicate though. Hope this helps! 123dsadqwe 0 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

Think of BindableEvents like RemoteEvents, but only seen by the server. They can be :Fire()ed and :Connect()ed to just like RemoteEvents and other events can, same goes for BindableFunctions but with :Invoke() and .OnInvoke.

You might ask "But why would I ever need an event only viewable by the server?"

Server-sided events can be useful in a number of ways, for example: let's say you have a round-based game, you might have many scripts that need to know the current state of the round (in progress or not) to execute specific functions at those times. Sure, you could use a BoolValue and have those scripts listen for the Changed event, but you're essentially doing the same thing as you would with a BindableEvent, just with a few extra steps. Instead, you could have two BindableEvents in ServerScriptService, one for the round starting and one for ending. When whatever condition that starts the round has been met (for instance player count) you could :Fire() the event to let any other scripts that are :Connect()ed to it know the round is starting. EX: :Fire()ing script:

local Players = game:GetService("Players")
local ServerScripts = game:GetService("ServerScriptService")
local playerCount = #Players:GetPlayers()

Players.PlayerAdded:Connect(function()
    playerCount += 1
    if (playerCount >= 4) then
        local event = ServerScripts.Events.ROUND_START --grab event from a folder
        event:Fire() --fire the event
    end
end)

--You'd of course put other code here so that the playerCount can actually go down, but this is just for an example

:Connect()ed script:

local ServerScripts = game:GetService("ServerScriptService")
local roundStart = ServerScripts.Events.ROUND_START --get event from folder

roundStart.Event:Connect(function()
    --some code here that will execute when this event is :Fire()ed
end)

And just like regular roblox events and RemoteEvents, BindableEvents can also accept arguments, EX:

local someEvent = game.ServerScriptService.Events.something --get event from folder

someEvent.Event:Connect(function(text)
    print(text) --this will print whatever we provide as the "text" parameter
end)

someEvent:Fire("Hello World!") --this will make the event print "Hello World!" in the console, along with executing anything else that is :Connected to the event

As you can see, BindableEvents can be extremely useful for communication between server scripts.

Now for BindableFunctions: The difference between a BindableEvent and a BindableFunction is that BindableFunctions can store a value (a function) whereas BindableEvents store nothing, they just send off a "ping" whenever they are :Fire()d to any :Connect()ed scripts. BindableFunctions can come in handy when you want to allow several scripts to use the same function without copy pasting it into all of them (which would violate DRY -- Don't Repeat Yourself)

For example:

local bf = game.ServerScripts.Functions.someBindableFunction --grab the BindableFunction from a folder

bf.OnInvoke = function()
    return 12
    --this function will be called every time another script calls :Invoke() on it
end

local something = bf:Invoke()
print(something) --output: 12

And of course, due to the OnInvoke value being a function, it can also accept arguments. I personally don't really use BindableFunctions as I feel ModuleScripts do their job a bit better (In case you don't know, ModuleScripts can hold many values including functions which can then be "imported" into other scripts. More info here)

For more information regarding BindableFunctions and BindableEvents: BindableFunction, BindableEvent

I hope you understand BindableFunctions and BindableEvents a bit better now.

Ad

Answer this question