Hello DevForum, I have come to ask how I can better make my current weighted luck system, the script below is what I currently have and I am looking to “optimize” for lack of a better word.
Also, the rarities are just for testing. Don’t question them.
local module = {}
module.rarityParts = {
["Legendary"] = {
game.Workspace:WaitForChild("Legendary")
},
["Rare"] = {
game.Workspace:WaitForChild("Rare")
},
["Uncommon"] = {
game.Workspace:WaitForChild("Uncommon")
},
["Common"] = {
game.Workspace:WaitForChild("Common")
}
}
module.rarityTest = {
["How"] = 0.00018647119,
["Legendary"] = 9.99998135288,
["Rare"] = 15, -- 1/100
["Uncommon"] = 25,
["Common"] = 50,
}
module.chooseRandRarity = function()
local randomNum = math.random(1, 100)
local counter = 0
for rarity, weight in pairs(module.rarityTest) do
counter = counter + weight
local nCounter = math.floor(counter)
if randomNum <= nCounter then
local rarityTbl = module.rarityParts[rarity]
local chosenRarity = rarityTbl[math.random(1, #rarityTbl)]
return chosenRarity
end
end
end
return module