So i saw both of theese and i don't know about it and what they are used and their differences is.
Think of BindableEvent
s like RemoteEvent
s, but only seen by the server. They can be :Fire()
ed and :Connect()
ed to just like RemoteEvent
s and other events can, same goes for BindableFunction
s 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 BindableEvent
s 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, BindableEvent
s can be extremely useful for communication between server scripts.
Now for BindableFunction
s:
The difference between a BindableEvent
and a BindableFunction
is that BindableFunction
s can store a value (a function) whereas BindableEvent
s store nothing, they just send off a "ping" whenever they are :Fire()
d to any :Connect()
ed scripts.
BindableFunction
s 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 BindableFunction
s as I feel ModuleScript
s 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 BindableFunction
s and BindableEvent
s:
BindableFunction,
BindableEvent
I hope you understand BindableFunction
s and BindableEvent
s a bit better now.