RemoteFunctions and RemoteEvents are used for communicating code through client and server.
A RemoteFunction, like a RemoteEvent, is an object that allows communication between client and server. It is different from a RemoteEvent in that i can return a value, which yields the invoking code until it recieves a response (Be it a returned value or the finishing of code). RemoteFunctions are called using the the methods InvokeServer
and InvokeClient
InvokeClient
should be called from the server, and InvokeServer
from the client
On the other side of the call, the receiving end, you would use the callbacks OnClientInvoke
and OnServerInvoke
. OnClientInvoke
would be used on the client, while OnServerInvoke
would be used on the server
Code Example:
Server
01 | local ClientToServer = Instance.new( "RemoteFunction" ,workspace) |
02 | ClientToServer.Name = "Client to Server" |
03 | local ServerToClient = Instance.new( "RemoteFunction" ,workspace) |
04 | ClientToServer.Name = "Server to Client" |
05 | ClientToServer.OnServerInvoke = function (Argument 1 ) |
07 | return "Successfully communicated with the server!" |
09 | local WhatToReturn = ServerToClient:InvokeClient( "This will show up on the client!" ) |
Output:
This will show up on the server!
Successfully communicated with the client!
Client
1 | local ClientToServer = workspace:WaitForChild( "Client to Server" ) |
2 | local ServerToClient = workspace:WaitForChild( "Server to Client" ) |
3 | ServerToClient.OnClientInvoke = function (Argument 1 ) |
5 | return "Successfully communicated with the client!" |
7 | local WhatToReturn = ClientToServer:InvokeServer( "This will show up on the server!" ) |
Output:
This will show up on the client!
Successfully communicated with the server!
A RemoteEvent is an object similar to a RemoteFunction, though it does not return anything. RemoteEvents are called using FireServer
and FireClient
. FireServer
is to be used from the client, while FireClient
is used on the server.
When called, the RemoteEvent uses it's callbacks OnClientEvent
and OnServerEvent
. OnServerEvent
is used from the server, OnClientEvent
from the client.
Code Example:
Server
1 | local ClientToServer = Instance.new( "RemoteEvent" ,workspace) |
2 | ClientToServer.Name = "Client to Server" |
3 | local ServerToClient = Instance.new( "RemoteEvent" ,workspace) |
4 | ClientToServer.Name = "Server to Client" |
5 | ClientToServer.OnServerEvent = function (Argument 1 ) |
8 | ServerToClient:FireClient( "This will show up on the client!" ) |
Output:
This will show up on the server!
Client
1 | local ClientToServer = workspace:WaitForChild( "Client to Server" ) |
2 | local ServerToClient = workspace:WaitForChild( "Server to Client" ) |
3 | ClientToServer:FireServer( "This will show up on the server!" ) |
4 | ServerToClient.OnClientEvent = function (Argument 1 ) |
Output:
This will show up on the client!