After I required the module and have the rarities, I want the most common ones to become more rare so you have better chances to get the higher ones and slowly move up the ladder.
For some reason I do not have an idea on how to do this.
Note: I deleted my code, I will redo it, that is why no code is included here.
To make the system more basic, you could set it up a bit like this:
return {
["Common"] = 2;
}
And have in your server script something like this:
local stats = require(modulescriptlocation)
local newNum = math.random(1,100)
local lastStat = 0
local curItem = nil
for i,v in pairs(stats) do
if v <= newNum and v > lastStat then
curItem = v
end
end
Look, this will have some flaws, but I am just trying to show you a bit of a picture. As long as the rarities were in between 1-100 it would somewhat work. It would not work with duplicate values though. But as I said, I am just trying to give you a basic idea.
I understand that part, but thanks for the explanation. I guess I probably did not explain what I want in a proper way, sorry about that. I what I want is a luck boost integrated with a weighted random system. I am just unsure on what the logic behind the luck boost system would be.
The system is weighted. All you need to do is to operate with the math.random(#weightSum), then compare the random number with the table in a specific way(and it seems that it could be recursive).
Perhaps you could do it in terms of probabilities?
Suppose I have n tiers of rarity, and P(x) is the probability of getting an item of x rarity.
Then, P(Common) + P(Uncommon) + … + P(Godlike) = 1. We are essentially setting a probability distribution for each tier of rarity.
We can use a table structure similar to what you have right now:
local function getTable()
return {
[1] = "Rare"
[2] = "Uncommon"
[5] = "Common"
}
end
Here, we can take P(Common) = 5/(1+2+5) = 5/8,
P(Uncommon) = 2/8 = 1/4
P(Rare) = 1/8
We then draw a random number from 1 to 8, then get the rarity based on whatever number that we rolled.
local pTable = getTable()
local number = math.random(1,8)
local function rollRarity()
for i, v in pairs(pTable) do
if(number <= i) then
return v
end
end
Note that the order of iteration is important here. The smallest keys (rarest) must always be iterated first, so make sure that your table is constructed from rarest to most common, as I believe Lua will iterate the table based on how you inserted it by default.
The reason the smallest keys must be iterated first is simple:
Suppose we roll a 2, our rarity should be “Uncommon”. But if we checked the entry 5 first in our for loop, the condition will still be true as 2 < 5, and we will mistakenly return the rarity as “Common” when we actually rolled an “Uncommon”.
These are my thoughts on how the chances of getting a rare item can be implemented. If you want to increase the chances of getting a rarer item, then you can basically change the distribution numbers so that the P(x) for whatever tier rarity you want is increased relative to the rest. One simple way I can think of is to subtract a particular amount (based on luck stat?) from the rolled number to give the user a higher chance of hitting the threshold to get a rarer item.
Thanks this might actually be a better system to understand since it has more understandable math linked to it in my opinion.
I have a query though, a possible solution for my problem, but im unsure if it will be a good solution.
This will be done with your script:
local number = math.random(1,8) --the way you get the random number
--wont this be an efficient and easy solution?
local number = math.random(math.random(1, 1*luckboost), 8)
-- Default luck boost would be 1 of course and will not exceed the max of 8 in this case.
Here, the smaller the number that we roll, the better rarity that we should get. In your code, it will end up making the luck worse instead. This could work:
local number = math.random(1, math.random(8/luckboost, 8))
Notice how the luckboost tries to reduce the upperbound of the number we roll.
Oh yeah sorry, this was something I thought of with the weighted system where the numbers were the most common was the most small. So I accidentally inverted it.
Thank you for the help.
local function rollRarity()
local check = 0
for i, v in pairs(pTable) do
check = check + i
if(number <= check) then
return v
end
end
So now when we iterate, check will have the following values: 1 → 3 → 8 which corresponds with our distribution.
P(“Rare”) will be 1 out of 8 (we rolled 1), P(“Uncommon”) will be 2/8 (we rolled either 2 or 3), and P(“Common”) will be 5/8 (we rolled a number from 4 to 8 inclusive)