Codectionary / Developer documentation / Luau

RemoteFunction and yielding requests

RemoteFunction is a request/response operation: InvokeServer yields until the server returns. Use it for a small, bounded query when the caller needs a result. Avoid making essential server progress depend on invoking an untrusted client.

Syntax

local ok, result = pcall(function()
    return remote:InvokeServer("catalog")
end)

Examples

Serve a bounded query

Create a RemoteFunction named LessonCatalog in ReplicatedStorage. Script in ServerScriptService. The response is server-defined and contains no private state; one request per player per second is accepted.

local Players = game:GetService("Players")
local remote = game:GetService("ReplicatedStorage"):WaitForChild("LessonCatalog")
local last = {}
remote.OnServerInvoke = function(player, action)
    if action ~= "catalog" then return nil end
    local now = os.clock()
    if now - (last[player] or -math.huge) < 1 then return nil end
    last[player] = now
    return {"Types", "Tables", "Events"}
end
Players.PlayerRemoving:Connect(function(player) last[player] = nil end)

Handle failure and a rejected response

LocalScript in StarterPlayerScripts. Run with the server example. pcall catches an error but does not impose a timeout on InvokeServer; keep the server handler short and avoid repeated calls every frame.

local remote = game:GetService("ReplicatedStorage"):WaitForChild("LessonCatalog")
local ok, result = pcall(function()
    return remote:InvokeServer("catalog")
end)
if ok and typeof(result) == "table" then
    print(table.concat(result, ", "))
else
    warn("Catalog unavailable; try again later")
end

Best practices

  • Assign one OnServerInvoke callback; it is not a signal with Connect.
  • Use RemoteEvent for notifications that need no return value.
  • Do not call DataStores on every UI query; serve validated session data and use an explicit loading state.

At a glance

Purpose
Typed scripting and Roblox development
File extension
.luau
Runs in
Luau host; Roblox engine examples require Roblox Studio
Usually used with
Roblox APIs and Studio

Specifications & further reading

Related Luau documentation