Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

I need some help with remotefunctions and remoteevents?

Asked by 8 years ago

I looked at the wiki, it's sorta confusing. Any examples / models / scripts / explanations can help me. Please and thanks!

1 answer

Log in to vote
2
Answered by 8 years ago

Introduction

RemoteEvents and RemoteFunctions are ways to communicate with opposite sides of the network (the network being the client-server model your game operates on). The concept of having these remotes allow communication between a client and server is nothing new, so there's no "definite" explanation on how to use them in all cases, since they'll differ depending on the system you're working with. But, that's getting into a broader topic, let's stick to ROBLOX-specific remotes.

All remotes, and their purpose

We've already mentioned the first two: RemoteEvents and RemoteFunctions, but there's technically two more. The reason I say "technically" is because they don't function on both sides of the network, but rather the side the listener has been created on. These "remotes" are referred to as: BindableEvents and BindableFunctions. (In case you've never seen what they look like, here: http://prntscr.com/ap76tw)

What's the difference between RemoteEvents and RemoteFunctions?

Like I said before, both of these remotes are dependent on a connection to the server. However, they do have some important differences. For example, when you fire a RemoteEvent, that information will be passed to the server, without the client waiting for any feedback from the server. This is done via the FireServer method on the RemoteEvent. In other words, the client won't wait for the server to respond. It just sends the information, assuming it successfully reached. This can be very useful for sending information for the server to store, or maybe running a few quick commands sent from the client, for the server to execute (or obviously anything else, you're not really limited to what you can use them for).

RemoteFunctions on the other hand, will wait for feedback from the server before resuming the rest of your script. Because of this, RemoteFunctions give you the ability to have the server return information to the client, once the client has invoked the remote. This is done via the InvokeServer method on the RemoteFunction.

What're the similarities between RemoteEvents and RemoteFunctions?

Both RemoteEvents and RemoteFunctions send information to the server, and both have a default first argument on the listener of the remote, which is the player of the local script that activated the listener (this is of course assuming the listener has been created on the server)

What is a listener?

The "listener" I keep talking about is what our remotes wait for until they're activated by a client (or server, if the listener is on the client). Remember, the listener must be on the opposite side of the network of what's trying to activate the remote. Think of it as being on the opposite end of a phone line:

Client is listening for the call ---- > Server activates the call

Likewise vise-versa...

Server is listening for the call ---- > Client activates the call

We can create a RemoteEvent listener via the OnServerEvent event, by including the :connect(Function) method to specify the code that runs when the listener is called. Here's an example using a RemoteEvent:

-- "OnServerEvent" connected with our anonymous function with two arguments: "Player" and "FirstArgument"

-- Remember, server-side listeners have their first parameter set by default, which is the player of the local script that called the listener. 
Remote.OnServerEvent:connect(function(Player,FirstArgument)
    print(Player,FirstArgument)
end)

-- Somewhere in a local script...
Remote:FireServer("Hello") -- This argument will be passed to the "FirstArgument" parameter in the listener created above

The code above would print the player's name, followed by the word "Hello", showing that the first parameter is indeed occupied by the client. If we wanted to create a server-sided RemoteFunction listener however, we'd use OnServerInvoke which is a separate callback function from typical ROBLOX events that use :connect(). We'd set this up the same way we would any ordinary function in a table:

-- Same rules apply here with RemoteEvents, as far as parameters go.
RemoteFunction.OnServerInvoke = function(Player,FirstArgument)
    print(Player,FirstArgument)
    return "Returned this value"
end

-- Calling the listener somewhere in a local script...
print(RemoteFunction:InvokeServer("Hello")) -- Print what our RemoteFunction returns to this call, which will the be string "Returned this value"

Sources

My old chat system video implementing the use of RemoteEvents: https://www.youtube.com/watch?v=uoUfrm9ZUbI

Client-server model: https://en.wikipedia.org/wiki/Client%E2%80%93server_model

Questions

Hope this helped a bit, I'd go more in depth but I'm afraid my answer is too long for the site to accept. But let me know if you have any questions, I'd be happy to give some examples.

Ad

Answer this question