Lootbag
Hi! This is my second Open-source module!, I was helping my friend with RNG system that accept luck
and We wanted to see if we can do something that’s different from existing modules
so after a while We figured something out and made one!
This Lootbag is slightly different from other weighted systems on how it use luck
Basic overview
local Lootbag = require(...)
local rarity = Lootbag.new()
-- adding items (1/2, 1/4, 1/8 , 1/16, ...) :: weight that isn't decimal works, I just do this for example
for index, value in {'Common', 'Uncommon', 'Unusal', 'Remarkable', 'Rare', 'Outstanding' ,'Exceptional' ,'Unique', 'Epic', 'Legendary'}do
rarity:AddItem(value, 1 / (2 ^ index))
end
-- GetItem() optionally accept luck and reroll
print(rarity:GetItem()) -- Common, 0.5, 1 (item, weight, index of item in lootbag.Items)
print(rarity:GetItem(2)) -- Unusal, 0.25, 2
-- Sample entire Lootbag, also optionally accept luck and reroll
rarity:Sample(1e6, 2, 1) -- count, luck, reroll
--[[
Sample's output:
{item} : {count} ( {actualPercentage} : {samplePercentage} )
[Lootbag]: 1000000 Samples (Luck:2, Retries:0)
Common: 315628 [50.05% : 31.56%]
Uncommon: 304508 [25.02% : 30.45%]
Unusal: 181904 [12.51% : 18.19%]
Remarkable: 97417 [6.26% : 9.74%]
Rare: 50667 [3.13% : 5.07%]
Outstanding: 25697 [1.56% : 2.57%]
Exceptional: 12732 [0.78% : 1.27%]
Unique: 6573 [0.39% : 0.66%]
Epic: 3251 [0.20% : 0.33%]
Legendary: 1623 [0.10% : 0.16%]
]]
-- returning true will remove that item from lootbag
rarity:RemoveIf(function(item)
return item == 'Exceptional'
end)
API & Other stuffs
I did write API in the source itself and made type annotation
but if you want to check, Here is it
APIs
Lootbag.new() → Lootbag
returns Lootbag object
Lootbag:AddItem(item: any, weight: number) → nil
add item to Lootbag
Lootbag:GetItem(luck: number?, retries: number?) → any, number, number
get random item return item, weight and index of that item (inside Lootbag.Items)
Lootbag:GetItems(count: number?, luck: number?, retries: number?) → Items
do multiple GetItem() then return Items table as result
How table look like?
{
[item]: {
Weight: number,
Value: number,
},
...
}
Lootbag:Sample(count: number?, luck: number?, retries: number?, format: function?) → nil
sample by getting {count} items from lootbag using provided luck and retries then printing entire result in output
additionally accept format to format how item would be display in stringformat: (item) → string
Lootbag:ListItems(usePercentage: boolean?) → {[item]: number}
returns table that has items as key and weight or percentage as value
Lootbag:SetItem(item: any, weight: number) → nil
similar to AddItem but would overwrite the same exact item in lootbag
Lootbag:RemoveItem(item: any) → nil
remove the same exact item in lootbag
Lootbag:RemoveIf(predicate: function?) → nil
remove item if predicate(item) return true
predicate: (item) → boolean
Note
I do want to explain how it works but unfortunately I’m no where near qualify to do so
as It was my friend that came up with formula
and about the github I actually made Wally/Rotriever file myself as I’m not using them
I’m under impression that packages manager use toml/json to get the repositories
If it doesn’t work that’s probably why
If you found any bugs or packages doesn’t work please tell me
lastly Module’s result is a bit off compared to other ones
but please give it a try and tell me if you like it or not
I hope you find this useful