Skip to content

RemoteFunction

Lets a player’s device call the game server and wait for an answer.

  • Inherits: Instance
  • Creatable: Yes: new RemoteFunction(), or Instance.new('RemoteFunction', parent).
  • Replication: Replicated: clients get a copy, and changes the server makes reach them.

A LocalScript calls await remote.invokeServer(...); the server answers with the function you assign to onServerInvoke, which receives the calling player first. Arguments and the return value cross the network like RemoteEvent arguments, with the same limits, and count toward the same 60 calls a second.

The call rejects with an error when:

  • the server’s handler throws (the error’s message is passed along);
  • the server has no onServerInvoke for it;
  • the handler takes longer than 5 seconds of game time;
  • the player is over the rate limit;
  • no answer arrives within 10 seconds, or the player disconnects.

There’s no server-to-player invoke: use a RemoteEvent, and another one for the reply.

Type: ((player: Player, ...args: unknown[]) => unknown) | null · Access: read/write

Server: set this to answer; it may be async. The first argument is the calling player. When the script that set it stops, it’s cleared (unless something else set it since).

A RemoteFunction has one handler: assigning replaces the previous one. Treat the arguments as untrusted, like RemoteEvent arguments.

Inherited from Instance: name, archivable, id, className, parent, destroyed

invokeServer(...args: unknown[]): Promise<unknown>

Player’s device: calls the server’s onServerInvoke with these arguments and resolves with what it returns. Only works on a player’s device.

Inherited from Instance: getPropertyChangedSignal, setAttribute, getAttribute, getAttributes, getAttributeChangedSignal, getChildren, getDescendants, findFirstChild, findFirstChildOfClass, findFirstChildWhichIsA, findFirstAncestor, findFirstAncestorWhichIsA, waitForChild, isA, isDescendantOf, isAncestorOf, getFullName, clearAllChildren, destroy, clone, toString

Inherited from Instance: changed, childAdded, childRemoved, descendantAdded, descendantRemoving, ancestryChanged, destroying, attributeChanged

import { RemoteFunction, ReplicatedStorage, getChild } from '@rally/sdk';
// Script
const getPrices = getChild(ReplicatedStorage, 'GetPrices', RemoteFunction);
const prices: Record<string, number> = { hat: 50, cape: 120 };
getPrices.onServerInvoke = (player, item) => {
if (typeof item !== 'string' || !(item in prices)) throw new Error('unknown item');
return prices[item];
};
import { RemoteFunction, ReplicatedStorage, print, task, waitForChild } from '@rally/sdk';
// LocalScript
task.spawn(async () => {
const getPrices = await waitForChild(ReplicatedStorage, 'GetPrices', RemoteFunction);
try {
const price = await getPrices.invokeServer('hat');
print('a hat costs', price);
} catch (e) {
print('could not get the price:', e);
}
});