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

What are RemoteFunctions/RemoteEvents, and how/why would I use them?

Asked by
Soliate 80
9 years ago

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.

0
it a nutsheel RemoteFunctions Can return while RemoteEvents dont RM0d 305 — 9y
0
#NutShell MessorAdmin 598 — 9y

1 answer

Log in to vote
9
Answered by
Muoshuu 580 Moderation Voter
9 years ago

RemoteFunctions and RemoteEvents are used for communicating code through client and server.

RemoteFunction

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

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!


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!


RemoteEvent

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

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!


Client

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!

3
There is another key difference: RemoteFunctions yield the invoking code, and RemoteEvents do not. adark 5487 — 9y
3
Forgot to add that, I was in a hurry. Edited. Muoshuu 580 — 9y
0
bookmarked. 0xDEADC0DE 310 — 9y
Ad

Answer this question