RemoteFunction
An object which facilitates synchronous, two-way communication across the client-server boundary. Scripts invoking a RemoteFunction yield until they receive a response from the recipient.
Memory category | Instances |
---|
Member index 8
Description
The RemoteFunction object facilitates synchronous, two-way communication across the client-server boundary. You can use it to define a custom callback function and invoke it manually by calling RemoteFunction:InvokeClient() or RemoteFunction:InvokeServer(). The code invoking the function yields until it receives a response from the recipient.
In order for both the server and clients to access a RemoteFunction instance, it must be in a place where both sides can see it, such as ReplicatedStorage, although in some cases it's appropriate to store it in Workspace or inside a Tool.
If the result is not needed, it is recommended that you use a RemoteEvent instead, since its call is asynchronous and doesn't need to wait for a response to continue execution. See Remote Events and Callbacks for code samples and further details on RemoteFunction.
Streaming Precautions
Note that if an invoked RemoteFunction creates an instance on the server, there is no guarantee that it exists on the client when the function returns. This is particularly important in places where instance streaming is enabled and when the created instances are BaseParts or Models, since parts that are far away from the player's character may not be streamed to the client, and models that are Atomic depend on whether their parts are streamed. Even if a model is Persistent, there may be some delay between the creation of the model and when it is replicated to the client.
Parameter Limitations
Any type of Roblox object such as an Enum, Instance, or others can be passed as a parameter when a RemoteFunction is invoked, as well as Luau types such as numbers, strings, and booleans, although you should carefully explore the limitations.
History 15
- 553 Add
- 553 Add
- 553 Add
- 553 Add
- 524 Change Parameters of OnServerInvoke from (player: Instance, arguments: Tuple) to (player: Player, arguments: Tuple)
- 483 Change Parameters of InvokeClient from (player: Instance, arguments: Tuple) to (player: Player, arguments: Tuple)
- 462 Change ThreadSafety of OnServerInvoke from to Unsafe
- 462 Change ThreadSafety of OnClientInvoke from to Unsafe
- 462 Change ThreadSafety of InvokeServer from to Unsafe
- 462 Change ThreadSafety of InvokeClient from to Unsafe
- 123 Add OnServerInvoke
- 123 Add OnClientInvoke
- 123 Add InvokeServer
- 123 Add InvokeClient
- 123 Add RemoteFunction
Members 8
InvokeClient
Parameters (2) | ||
---|---|---|
player | Player | |
arguments | Tuple | |
Returns (1) | ||
Tuple |
Invokes the RemoteFunction which in turn calls the OnClientInvoke callback. Since this method is used to communicate from the server to a client, it will only work when used in a Script.
Any type of Roblox object such as an Enum, Instance, or others can be passed as a parameter to InvokeClient(), as well as Luau types such as numbers, strings, and booleans, although you should carefully explore the limitations.
See Remote Events and Callbacks for code samples and further details on RemoteFunction.
Warning
In practice, the server does not often invoke the client, as clients typically do not have information that the server doesn't have, and actions that only a client can take, such as displaying a GUI, typically do not require a callback. As such, RemoteEvent:FireClient() is recommended as an asynchronous method that doesn't need to wait for a response to continue execution.
If you legitimately need to invoke a client from the server, note the following risks:
- If the client throws an error, the server throws the error too.
- If the client disconnects while it's being invoked, InvokeClient() throws an error.
- If the client doesn't return a value, the server yields forever.
Thread safety | Unsafe |
---|
History 3
- 483 Change Parameters of InvokeClient from (player: Instance, arguments: Tuple) to (player: Player, arguments: Tuple)
- 462 Change ThreadSafety of InvokeClient from to Unsafe
- 123 Add InvokeClient
InvokeServer
Parameters (1) | ||
---|---|---|
arguments | Tuple | |
Returns (1) | ||
Tuple |
Invokes the RemoteFunction which in turn calls the OnServerInvoke callback. Since this method is used to communicate from a client to the server, it will only work when used in a LocalScript.
If a returned result is not needed, it's recommended to use RemoteEvent:FireServer() instead, as its call is asynchronous and doesn't need to wait for a response to continue execution.
Any type of Roblox object such as an Enum, Instance, or others can be passed as a parameter to InvokeServer(), as well as Luau types such as numbers, strings, and booleans, although you should carefully explore the limitations.
See Remote Events and Callbacks for code samples and further details on RemoteFunction.
Thread safety | Unsafe |
---|
History 2
- 462 Change ThreadSafety of InvokeServer from to Unsafe
- 123 Add InvokeServer
OnClientInvoke
Parameters (1) | ||
---|---|---|
arguments | Tuple | |
Returns (1) | ||
Tuple |
This callback is called when the RemoteFunction is invoked with InvokeClient(). When the bound function returns, the returned values are sent back to the calling server.
See Remote Events and Callbacks for code samples and further details on OnClientInvoke.
Thread safety | Unsafe |
---|
History 2
- 462 Change ThreadSafety of OnClientInvoke from to Unsafe
- 123 Add OnClientInvoke
OnServerInvoke
Parameters (2) | ||
---|---|---|
player | Player | |
arguments | Tuple | |
Returns (1) | ||
Tuple |
This callback is called when the RemoteFunction is invoked with InvokeServer(). When the bound function returns, the returned values are sent back to the calling client.
See Remote Events and Callbacks for code samples and further details on OnServerInvoke.
Thread safety | Unsafe |
---|
History 3
- 524 Change Parameters of OnServerInvoke from (player: Instance, arguments: Tuple) to (player: Player, arguments: Tuple)
- 462 Change ThreadSafety of OnServerInvoke from to Unsafe
- 123 Add OnServerInvoke