When Will I Use Bindable Events/Functions And Is It Necessary?

As the title says, why do I have to use it if I can just jam it into one script?

1 Like

It’s useful if you need to send information between scripts or functions.

One example is with BindToClose, which is often used to give the server time to save data before it shuts down. The two main ways of making sure it saves are:

  • Save each person’s data twice, once with a normal PlayerRemoving and once with the BindToClose

  • Have a BindableEvent to :Wait on in the BindToClose while the PlayerRemoving function runs as normal and then triggers the Bindable’s event.

There are lots of situations like this where they’re useful, this is just a simple example.

2 Likes

They’re still useful if you want to create your own event-based structure.

For example a lot of round-based games do stuff like this:

print("Waiting for players to join...")
while (#game.Players:GetPlayers() < 2) do
	print("Not enough players...")
	wait(1)
end
print("Enough players are here, starting game!")

But that can often be replaced with something like this:

local startGameEvent = Instance.new("BindableEvent")
game.Players.PlayerAdded:Connect(function()
	if (#game.Players:GetPlayers() >= 2) then
		startGameEvent:Fire()
	end
end)

print("Waiting for players to join...")
if (#game.Players:GetPlayers() < 2) then
	print("Not enough players...")
	startGameEvent.Event:Wait()
end
print("Enough players are here, starting game!")

The code looks longer, but waiting for events is much more preferable to polling.

4 Likes

ooh, I see. So I use bindables if I want to make a script shorter?? And can bindable events/functions do client to client?

1 Like

Yes, bindables are for server to server and client to client communication.

They are useful for custom events, but for sending and receiving data between scripts I prefer to use modules. Because I find it much harder to trace where a BindeableEvent is going.

I would never recommend just jamming everything in one script as it’s going to become an eyesore overtime to keep track of everything. But I feel like if you want to use Bindeable Events/Functions you might as well just adjust yourself to using ModuleScripts. Which you can trace back through the requires.

Yeah I still don’t understand. Like, what are bindables even for??

What @blokav is a common use for them, because as it’s currently made, their is no way to add your own events to your custom classes (OOP). So this can make for a workaround.

1 Like

Oh, so like you can use it to communicate and share data between scripts??

Yes, they can function to do that, and that’s their main purpose. But most people find it better to just use ModuleScripts which handle multi script workflows much better.

But for programmers who make their own classes/services it’s not possible to add your own Events similar to classes/services built into the API. So this provides for a workaround.

They are necessary for script to script communication but there are better options that make them not as effective.

1 Like