Server Rate Limiter Module

A reusable server-side Luau token-bucket limiter for controlling how often a player or key can request an action in your own experience.

Roblox Studio ModuleScript

What this script does

Refill tokens over time and reject requests that exceed a documented capacity without relying on a client-side cooldown.

Best for
Protecting RemoteEvents, purchases, pickups, and repeat buttons from accidental or abusive request bursts.
Language
Luau
Version
1.0.0
Validation
Luau runtime tested
Luau runtime tested

Official Luau 0.735 parsing and strict analysis pass, and the module is covered by the pure-module runtime behavior harness.

Included behavior

  • Token-bucket behavior
  • Per-key state and reset
  • Injectable clock for tests

Install in the stated runtime

  1. 1
    Create a ModuleScript named RateLimiter.
  2. 2
    Paste the complete source and require it from a server or test Script in your own experience.
  3. 3
    Use a user id plus action name as the key, then validate the payload only after allow returns true.

Minimal usage example

local RateLimiter = require(script.Parent.RateLimiter)
local limiter = RateLimiter.new(5, 1)
if limiter:allow(tostring(player.UserId) .. ":upgrade") then
	print("Request accepted")
end

Example inputs and result

Supply
A limiter configuration and a stable key for each action or player.
Expect
allow returns a boolean; only your server should decide whether to process the action.

These notes explain the example above. Read the complete source for every exported function and edge case before integrating it.

Complete source

No hidden import, minified block, or shortened loader.

--!strict
-- MIT License. Server-side token-bucket limiter for your own RemoteEvents.

type Bucket = { tokens: number, updatedAt: number }
export type RateLimiter = {
	capacity: number,
	refillPerSecond: number,
	buckets: { [string]: Bucket },
	allow: (self: RateLimiter, key: string, cost: number?, now: number?) -> boolean,
	reset: (self: RateLimiter, key: string) -> (),
}
local RateLimiter = {}
RateLimiter.__index = RateLimiter

function RateLimiter.new(capacity: number, refillPerSecond: number): RateLimiter
	assert(capacity > 0 and refillPerSecond >= 0, "Invalid limiter settings")
	return setmetatable({ capacity = capacity, refillPerSecond = refillPerSecond, buckets = {} :: { [string]: Bucket } }, RateLimiter) :: any
end

function RateLimiter.allow(self: RateLimiter, key: string, cost: number?, now: number?): boolean
	local currentTime = now or os.clock()
	local requested = math.max(0, cost or 1)
	local bucket = self.buckets[key] or { tokens = self.capacity, updatedAt = currentTime }
	local elapsed = math.max(0, currentTime - bucket.updatedAt)
	bucket.tokens = math.min(self.capacity, bucket.tokens + elapsed * self.refillPerSecond)
	bucket.updatedAt = currentTime
	self.buckets[key] = bucket
	if bucket.tokens < requested then return false end
	bucket.tokens -= requested
	return true
end

function RateLimiter.reset(self: RateLimiter, key: string) self.buckets[key] = nil end
return RateLimiter
SHA-2563299a75caebfdc0012874a030ad090c9cb5378f2be3700a2c0cd9ceaf365d1c6

Outside this script’s scope

  • Ban or identify exploiters
  • Validate the request payload

Found a problem or a better approach?

Tell us what failed, what you expected, and whether the issue happened in the Luau CLI or Roblox Studio. Suggestions for clearer types, safer server checks, or a more useful API are welcome.

Do not submit credentials, private project files, or account data.