A lightweight, strongly-typed Luau utility for uniform and weighted random selection. Designed for Roblox, LootTable provides a stable and performant way to handle randomized selections with support for runtime mutations.
| 💡 Note: While designed for Roblox, this is written in pure Luau and can be used outside of the Roblox engine.
- 🎲 Uniform & Weighted Selection: Supports simple arrays for equal probability and dictionaries for custom weights.
- 🔄 Stable Ordering: Guarantees deterministic iteration order internally for predictable behavior.
- ⚡ Performant: Optimized selection logic with O(1) removals via swap-back.
- 🛠️ Mutable: Add or remove items dynamically at runtime.
- 🟦 Strictly Typed: Full Luau type support for a better developer experience.
Download the latest LootTable.rbxm from the Releases page and drag it into your Roblox Studio project and move the contained module into your desired location (typically into ReplicatedStorage or ServerStorage).
- Create a new
ModuleScriptnamedLootTable. - Copy the source code from
LootTable.luauand paste it into the script.
local LootTable = require(path.to.LootTable)
local rarityTable = LootTable.new({"Common", "Uncommon", "Rare"})
local result = rarityTable:getRandomItem()
print(result) -- Equal 33.3% chance for eachlocal weights = {
["Common"] = 70,
["Uncommon"] = 25,
["Legendary"] = 5,
}
local loot = LootTable.new(weights)
local item = loot:getRandomItem()
print(`You rolled: {item}`)If you need both the item and its calculated probability:
local entry = loot:getRandomItemWithWeight()
if entry then
print(`Dropped {entry.item} with a {entry.weight * 100}% chance!`)
endloot:addItem("Mythic", 1) -- Add a new item with weight 1
loot:removeItem("Common") -- Remove an item at runtimeCreates a new LootTable instance from either an array or a weighted dictionary.
| 💡 Note: While technically set up to accept strings, you can use Roblox Instances directly as well.
Returns a randomly selected item.
Returns a randomly selected item along with its normalized probability (0 to 1).
Adds a new item to the table. If it's a weighted table, you can specify the weight (defaults to 1).
Removes an item from the table. Returns true if successful.
Returns whether the table contains the specified item.
Returns an array containing all items in the table.
Returns a list of all entries sorted by their probability (ascending).
Developed by Distracted Games