What this script does
Store cloned egg records by id so duplicate ids and over-capacity additions fail before they reach your own UI or save layer.
- Best for
- An original egg collection, placement, or hatch prototype in Roblox Studio.
- Language
- Luau
- Version
- 1.0.0
- Validation
- 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
- Unique-id records
- Capacity and duplicate checks
- Rarity summary counts
Install in the stated runtime
- 1Create a ModuleScript named EggInventory.
- 2Paste the complete source and require it from a server or test Script in your own experience.
- 3Create the inventory on the server and supply records produced by your own validated gameplay.
Minimal usage example
local EggInventory = require(script.Parent.EggInventory)
local eggs = EggInventory.new(8)
eggs:add({ id = "e1", name = "Striped Egg", rarity = "Rare" })
print(eggs:countByRarity().Rare)Example inputs and result
- Supply
- A capacity and Egg records with your own ids, names, and rarities.
- Expect
- countByRarity summarizes the Eggs currently held by this in-memory inventory.
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. Typed egg inventory model for a Studio experience you own.
export type EggRecord = { id: string, name: string, rarity: string, mutation: string? }
export type EggInventory = {
capacity: number,
records: { [string]: EggRecord },
count: (self: EggInventory) -> number,
add: (self: EggInventory, record: EggRecord) -> boolean,
get: (self: EggInventory, id: string) -> EggRecord?,
remove: (self: EggInventory, id: string) -> EggRecord?,
countByRarity: (self: EggInventory) -> { [string]: number },
}
local EggInventory = {}
EggInventory.__index = EggInventory
function EggInventory.new(capacity: number): EggInventory
return setmetatable({ capacity = math.max(0, math.floor(capacity)), records = {} :: { [string]: EggRecord } }, EggInventory) :: any
end
function EggInventory.count(self: EggInventory): number
local total = 0
for _ in self.records do total += 1 end
return total
end
function EggInventory.add(self: EggInventory, record: EggRecord): boolean
if record.id == "" or self.records[record.id] or self:count() >= self.capacity then return false end
self.records[record.id] = table.clone(record)
return true
end
function EggInventory.get(self: EggInventory, id: string): EggRecord? return self.records[id] and table.clone(self.records[id]) or nil end
function EggInventory.remove(self: EggInventory, id: string): EggRecord?
local record = self.records[id]
self.records[id] = nil
return record
end
function EggInventory.countByRarity(self: EggInventory): { [string]: number }
local counts: { [string]: number } = {}
for _, record in self.records do counts[record.rarity] = (counts[record.rarity] or 0) + 1 end
return counts
end
return EggInventory
aa2d01c90f6e1834b4702cde7e4e689c05fdb4ba2675e9d36a29d318a588baa0Outside this script’s scope
- Read Steal An Egg inventory
- Hatch, sell, or transfer live items
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.