Could I have some examples of BindableEvents and BindableFunctions

Could I have some examples of BindableEvents and BindableFunctions?
I find the API reference quite bland and hard to understand (the examples)
I know they are for Server-Server and Client-Client Messaging.
I just don’t really understand how to use them.


What I’m looking for?

  1. Example of code
  2. The function of that code
  3. Any other information that you want to add.

– NSG

2 Likes
--server
MyEvent.OnServerEvent:Connect(function(player, ...) --player is passed in automatically
   args = {...}
   print(args[1]
   print(args[2]) 
end)


--client
MyEvent:FireServer("apple", "banana") --the server will print apple and banana

Don’t forget to validate any data from the client.

Woops, I see that you want bindable functions…

3 Likes

I think you’re confusing RemoteEvents with BindableEvents. I believe you’re looking for .Event .

Additionally, FireServer should simply be :Fire()

You should also note that BindableEvents do not allow for communication between the server and client. If you are looking for this functionality use a RemoteEvent

1 Like

Oh my, I can’t believe I didn’t even notice that.

2 Likes

Well, the idea is very similar to remote events and remote functions. Try looking into those, as they are similar and essential in games. The difference is they cannot do client-server communications (or vice versa).

1 Like

so i dont know if this is a perfect example but here

local replicatedstorage = game:GetService("ReplicatedStorage")
local bindevent = replicatedstorage.bindableevent
local Combo= {
"Kick1"
"Punch1"
"StomachGut"

}

bindevent:Fire(Humanoid, [Combo])

so basically this is for if you have a animation module, your firing the humanoid, combo and then in the animation module you do this

local replicatedstorage = game:GetService("ReplicatedStorage")
local bindevent = replicatedstorage.bindableevent

bindevent.Event:Connect(function(Humanoid, [Combo])
print([Combo])

not sure if this works exactly correctly but this is what i use bindable events for, and i use bindable functions for my animation module too but it was for getting the track and playing it, i might add a example of it later

2 Likes

BindableEvents are a very powerful tool to use in your Roblox games, as you can not only share information between scripts, but also define your own events.

Let’s consider an example where you’re rendering a projectile on the client:

local rs = game:GetService("RunService").RenderStepped:connect(function(step)
    -- code
    if condition then -- this can be a bullet hitting something
        RenderEnd:Fire() -- BindableEvent
    end
end)

-- below line of code yields the thread until RenderEnd is fired
RenderEnd.Event:wait()

rs:disconnect() -- stops the function inside of RenderStepped

Using this method, you can exit RenderStepped much quicker than using a while loop, as explained in this topic by Kampfarren.

Another example can be making a round system, where GameEnd is a BindableEvent, without that awkward while true do strategy:

function startGame()
    -- game stuff
    GameEnd:Fire()
end

GameEnd.Event:connect(function()
    -- code
    startGame() -- start new game after intermission ends
end)

All that needs to be done is call startGame() initially.

I skimmed over this briefly with my explanations, but I hope you get where I’m going at.

2 Likes

So they are basically just RemteFunctions and RemoteEvents, but they can only have to contact between server scripts

Not exactly. Bindables are indeed like remotes but they only allow for communication on the same machine. Doesn’t have to be server scripts only. Local scripts can use them as well.

So only the same class of script can fire each other (Modules included?)