Hello, since I have never used a remote function/event I am wondering how they would work, if I could get an example along side with an explanation to help my understanding of how both these objects work.
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
local ClientToServer = Instance.new("RemoteFunction",workspace) ClientToServer.Name="Client to Server" local ServerToClient = Instance.new("RemoteFunction",workspace) ClientToServer.Name="Server to Client" ClientToServer.OnServerInvoke = function(Argument1) print(Argument1) return "Successfully communicated with the server!" end local WhatToReturn = ServerToClient:InvokeClient("This will show up on the client!") print(WhatToReturn)
Output:
This will show up on the server!
Successfully communicated with the client!
local ClientToServer = workspace:WaitForChild("Client to Server") local ServerToClient = workspace:WaitForChild("Server to Client") ServerToClient.OnClientInvoke = function(Argument1) print(Argument1) return "Successfully communicated with the client!" end local WhatToReturn = ClientToServer:InvokeServer("This will show up on the server!") print(WhatToReturn)
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.
local ClientToServer = Instance.new("RemoteEvent",workspace) ClientToServer.Name="Client to Server" local ServerToClient = Instance.new("RemoteEvent",workspace) ClientToServer.Name="Server to Client" ClientToServer.OnServerEvent = function(Argument1) print(Argument1) end ServerToClient:FireClient("This will show up on the client!")
Output:
This will show up on the server!
local ClientToServer = workspace:WaitForChild("Client to Server") local ServerToClient = workspace:WaitForChild("Server to Client") ClientToServer:FireServer("This will show up on the server!") ServerToClient.OnClientEvent = function(Argument1) print(Argument1) end
Output:
This will show up on the client!