Hello, I have a function which randomly picks an item from a table depending on its rarity
What I want: I want the function to use a number value as “Luck”, so basically the higher the luck the easier to get a more rare item, but i do not know how to do that!
Here is my script below:
local SwordModule = {
{model = script["Linked"], probability = 2, annouce = false},
{model = script["Ice Dagger"], probability = 3, annouce = false},
{model = script["Windforce"], probability = 5, annouce = false},
{model = script["Venomshank"], probability = 10, annouce = false},
{model = script["Ghostwalker"], probability = 50, annouce = false},
{model = script["Darkheart"], probability = 3, annouce = false},
{model = script["Hellsword"], probability = 2, annouce = true},
}
SwordModule.get = function(player)
local luck = 1 -- the more luck = easier to get rare items
local totalInverseProbability = 0
for _, item in ipairs(SwordModule) do
totalInverseProbability = totalInverseProbability + 1 / item.probability
end
local randValue = math.random() * totalInverseProbability
local accumulatedWeight = 0
for _, item in ipairs(SwordModule) do
accumulatedWeight = accumulatedWeight + 1 / item.probability
if randValue <= accumulatedWeight then
return item
end
end
end