Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 37 ยท Multiplayer and Security

RemoteFunctions and Input Validation

Use RemoteFunctions to get an answer from the server, learn why InvokeClient is dangerous, and write reusable validators and rate limiters for every remote.

A RemoteEvent is a message with no reply. Sometimes the client really needs an answer: "How many coins do I have?", "Is this item available?", "Can I buy this?". For that, Roblox offers the RemoteFunction. The client calls it and waits until the server returns a value, just like a normal function call that crosses the network.

This lesson shows how a RemoteFunction works, when to avoid it, and how to build the validation habits that protect every remote in your game. Roblox code is shown read-only; the runnable code is a JavaScript stand-in for the logic.

InvokeServer and OnServerInvoke

A RemoteFunction lives in ReplicatedStorage. The server defines one function to run, and the client calls it.

LUA
-- Server
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local getPrice = ReplicatedStorage.GetPrice

local PRICES = { Sword = 100, Shield = 60 }

getPrice.OnServerInvoke = function(player, itemName)
	if typeof(itemName) ~= "string" then
		return nil
	end
	return PRICES[itemName] -- nil if the item does not exist
end
LUA
-- Client
local price = getPrice:InvokeServer("Sword")
print("A sword costs", price)

Differences from a RemoteEvent:

  • OnServerInvoke is a property you assign, not an event you connect to. Only one function can be assigned.
  • InvokeServer yields (pauses the calling script) until the server returns. If the server errors, the client receives an error.
  • Return values are copied back to the client, with the same limits as event arguments.

Why you should avoid InvokeClient

The server can also call a client with remoteFunction:InvokeClient(player). Do not do this for anything important:

  • If the player leaves or the client never answers, the server script waits forever.
  • A malicious client can simply refuse to return, or return anything, and freeze the server thread.

If the server needs information from a client, ask for it with a RemoteEvent and handle the reply asynchronously with a timeout, or better, avoid needing it: let the server compute things itself.

Event or Function?

RemoteEvent Fire and forget. No answer.
RemoteFunction Ask and wait for an answer.
InvokeClient Can hang the server. Avoid.

Prefer events, use a function when the client needs a result.

1 / 2

Validation: the three questions

Every handler of a remote should answer three questions before it does anything:

  1. Is it the right shape? Check the type of every argument with typeof, and check numbers are finite (not NaN or infinite), integers where you expect them, and strings are not absurdly long.
  2. Is it allowed? Is this item in the shop? Does the player own it? Is the round running? Is the target within range?
  3. Is it too often? Has this player called the remote too many times lately (a rate limit)?
LUA
local function isValidQuantity(n)
	return typeof(n) == "number"
		and n == n          -- false for NaN
		and n > 0
		and n <= 99
		and n % 1 == 0      -- whole numbers only
end

The n == n trick works because NaN is the only value not equal to itself. Skipping such a check lets a client send NaN or math.huge, which can break your maths or inventory.

A validated purchase

Here is a full server handler for buying items. Look for the three questions.

LUA
local ITEMS = { Sword = 100, Shield = 60 }

buyRemote.OnServerInvoke = function(player, itemName, quantity)
	-- 1. Shape
	if typeof(itemName) ~= "string" or not isValidQuantity(quantity) then
		return false, "bad request"
	end
	-- 2. Allowed
	local price = ITEMS[itemName]
	if not price then
		return false, "unknown item"
	end
	local coins = player.leaderstats.Coins
	local total = price * quantity
	if coins.Value < total then
		return false, "not enough coins"
	end
	-- 3. (rate limit omitted here, see below)
	coins.Value -= total
	giveItem(player, itemName, quantity)
	return true, "ok"
end

Notice that the client never says what the item costs. It sends a name and a quantity, and the server looks up the price. If the client sent the price, an exploiter would just send zero.

Run the validators

This stand-in reproduces the same checks in JavaScript, and throws bad requests at it.

JS Validate a purchase request (JavaScript stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Only the two valid requests change coins; every malformed one is rejected, and the negative quantity cannot be used to "buy" for negative coins.

Rate limiting

A client can fire or invoke a remote hundreds of times a second. Even valid requests can overload the server or exploit timing. A simple rate limiter keeps a timestamp or a counter per player.

LUA
local lastCall = {} -- [player] = os.clock()

local function allow(player, minInterval)
	local now = os.clock()
	if now - (lastCall[player] or 0) < minInterval then
		return false
	end
	lastCall[player] = now
	return true
end

Players.PlayerRemoving:Connect(function(player)
	lastCall[player] = nil -- avoid a memory leak
end)

For bursty actions, a token bucket allows a few quick calls, then slows down. The next stand-in implements one.

JS A token-bucket rate limiter

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The first three calls pass as a burst, the next two are blocked, a second later one token has refilled, and Ben has his own separate bucket.

Checklist for every remote

  • Check the type and range of every argument.
  • Look up prices, damage and cooldowns on the server; never accept them from the client.
  • Verify the player is allowed (owns the item, is alive, is in the round).
  • Rate limit by player and clean up on PlayerRemoving.
  • Return simple results; never leak internal tables or error stacks.
  • Log suspicious requests so you can spot exploit attempts.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How does a client get an answer from the server through a remote?

    InvokeServer yields until the server's OnServerInvoke function returns a value.
  2. 2. Why is RemoteFunction:InvokeClient risky?

    The server thread waits for the client, and a malicious or disconnected client may never return.
  3. 3. Why should the server look up prices itself instead of receiving them from the client?

    Any value from the client can be forged, so the server should only accept identifiers and use its own data.
  4. 4. Which check detects a NaN number in Luau?

    NaN is the only value not equal to itself, so n ~= n is true only for NaN. (A positive test is n == n.)

Summary

  • A RemoteFunction lets a client call the server and wait for a return value; the server sets OnServerInvoke.
  • Avoid InvokeClient: it can freeze server code.
  • Validate shape, permission and frequency for every remote; reject anything unexpected.
  • Never accept prices, damage or identities from the client; the server owns all rules.
  • Rate limit per player, with a timestamp or a token bucket, and clear data on PlayerRemoving.

Next: Anti-Exploit Security