I seem to only ever be using RemoteEvents, but maybe anyone here can give me a practical example or something for what RemoteFunctions/BindableEvents be used for?
(I read the wiki and stuff about some uses, but nothing I cannot do without module scripts)
RemoteFunctions are similar to RemoteEvents but can return data back to the script that invoked it. This is useful in many cases, a big one being when the client needs to get player data and does not already have it on the client.
BindableEvents allow you to fire a RBXScriptSignal, which can then be seen by any other script that Connects to it. This allows for custom events to be created so that you do not have to constantly poll while waiting for something to happen.
I recommend reading these two articles for more information:
RemoteFunctions have quite a lot of practicality for me, mainly in battle systems.
Iām able to Invoke the server, have it yield and create everything while a loading screen is up and return back information on what to load. Additionally, I use them in shops to see if the player did or didnāt buy the item.
Moving onto BindableEvents, theyāre very handy for APIs inside your game though this can also be achieved through ModuleScripts as youāve said. The main use of them for me is an event to tell other UIs to open/close on the client however that can of course be done by a ModuleScript. Bindables overall are more preference related.
Other practical examples for RemoteFunctions is for purchasing items in a shop, because by using RemoteFunctions, the server can effectively tell the client if the purchase went through or not. With an event, the client has no way of knowing if the purchase went through without having the server send another event back to the client.
Keep in mind that a RemoteFunction can only have one callback tied to it, so only set the function once. It does this so that you do not have multiple functions returning values when you invoke the remote.
Also, it is recommended you avoid invoking a client from the server, as you will encounter an error if the client disconnects.
Forgot to add a really useful feature with BindableEvents and OOP. Say if you wanted to communicate to the above scope using the Object, thereās no real way accept your own kinda Event. You can define a property on the Object to be the BindableEvent.Event and have a name like āOnLoadedā, then inside the Object fire this BindableEvent and youāll be communicating easily.
A short example:
local Class = {};
Class.__index = Class;
function Class.new()
local LoadEvent = Instance.new('BinableEvent');
coroutine.wrap(function()
--// Load stuff
wait(2);
LoadEvent:Fire();
end)()
return setmetatable({
OnLoaded = BindableEvent.Event;
});
end
local CreatedClass = Class.new()
CreatedClass.OnLoaded:Connect(function()
print('Loaded!')
end)