Inventory Capacity Luau Module

A generic in-memory Roblox Studio inventory counter with capacity checks, item quantities, removal, totals, and snapshot helpers.

Roblox Studio ModuleScript

What this script does

Track item counts without Instances or client trust. Additions fail cleanly when the configured capacity would be exceeded.

Best for
Backpacks, egg capacity, crafting materials, and prototype storage rules.
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

  • Capacity-aware additions
  • Quantity removal and total count
  • Copy-safe snapshot output

Install in the stated runtime

  1. 1
    Create a ModuleScript named Inventory.
  2. 2
    Paste the complete source and require it from a server or test Script in your own experience.
  3. 3
    Create one server-side inventory per player session and persist only a validated snapshot.

Minimal usage example

local Inventory = require(script.Parent.Inventory)
local bag = Inventory.new(12)
print(bag:add("egg_basic", 3))
print(bag:total(), bag:canAdd(10))

Example inputs and result

Supply
A capacity, item id, and quantity for a server-owned bag.
Expect
add reports whether the items fit; total and canAdd inspect remaining capacity.

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. Generic in-memory capacity counter for your own Studio project.

export type Inventory = {
	capacity: number,
	counts: { [string]: number },
	total: (self: Inventory) -> number,
	canAdd: (self: Inventory, amount: number?) -> boolean,
	add: (self: Inventory, itemId: string, amount: number?) -> boolean,
	remove: (self: Inventory, itemId: string, amount: number?) -> boolean,
	snapshot: (self: Inventory) -> { [string]: number },
}
local Inventory = {}
Inventory.__index = Inventory

function Inventory.new(capacity: number): Inventory
	assert(capacity >= 0, "Capacity must be non-negative")
	return setmetatable({ capacity = math.floor(capacity), counts = {} :: { [string]: number } }, Inventory) :: any
end

function Inventory.total(self: Inventory): number
	local count = 0
	for _, amount in self.counts do count += amount end
	return count
end

function Inventory.canAdd(self: Inventory, amount: number?): boolean return self:total() + (amount or 1) <= self.capacity end
function Inventory.add(self: Inventory, itemId: string, amount: number?): boolean
	local addAmount = math.max(1, math.floor(amount or 1))
	if not self:canAdd(addAmount) then return false end
	self.counts[itemId] = (self.counts[itemId] or 0) + addAmount
	return true
end

function Inventory.remove(self: Inventory, itemId: string, amount: number?): boolean
	local removeAmount = math.max(1, math.floor(amount or 1))
	local current = self.counts[itemId] or 0
	if current < removeAmount then return false end
	self.counts[itemId] = current - removeAmount
	if self.counts[itemId] == 0 then self.counts[itemId] = nil end
	return true
end

function Inventory.snapshot(self: Inventory): { [string]: number } return table.clone(self.counts) end
return Inventory
SHA-2562f2bd1fad8e3d55b3dabd51d12286d1d99a4cf22ae1911c321f3292d7d745e03

Outside this script’s scope

  • Persist inventory automatically
  • Create UI or accept purchases

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.