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.
--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.
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
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).
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
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.
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.