# How to add luck to this Weighted / RNG system

Hello I am trying to add luck to this weighted luck system.

``````return {
getRandomIndex = function(playerLuck, chances)
for i, v in ipairs(chances) do
local randomNum = math.random(1, v[2])

if randomNum then

if randomNum == 1 and v[2] > chances[1][2] then
return {
v[1];
v[2];
v[3];
}
end
end
end
return chances[1]
end,
}
``````

So far its properly returning based on chances
This is in 1 Million roles

First Is 1 in 3
Second Is 1 in 5
Third Is 1 in 9

How would you add luck to it? I have a number value which is the player luck but I have no idea on how to boost the player chances of getting a â€śluckierâ€ť chance.

Any help / guidance is appreciated

3 Likes

You can probably create another RNG between like, half luck and whatever the luck stat is, and add the outcome to the final number if itâ€™s higher than the minimum luck needed to pass.

``````local luckModifier = math.random(luck * 0.5, luck)
local randomNumModified = randomNum + if luckModifier > "minimum luck" then ("amount to add") else 0
``````

Or maybe make the math.random range between luck and 100 (assuming luck doesnâ€™t cap out at 100. Then maybe 150 or 200? Something higher)

``````local luckModifier = math.random(luck, 100)
local randomNumModified = randomNum + if luckModifier > "minimum luck" then ("amount to add") else 0
``````
1 Like

Use weightedRandoms

Understand that your chance is the sum of the weighted values divided by value.

currentValue / ValuesCombined = PercentChanceOfSelection

1 Like

Shoutout to my open source module:

Anyways, an easy way is to multiply the weight of your item by itâ€™s luck factor. This is how I do mine:

``````itemWeight = itemWeight + (itemWeight * luckFactor)
``````
2 Likes

Also lume has a lot of very handy features to it, is 90% compatable with roblox and can teach you some interesting patterns and lua sugar like function chaining.