So I got this code from AI and I am unsure how to add a luck multiplier
local function GenerateRNG()
local totalWeight = 0
for Rarity, Data in pairs(Database) do
totalWeight = totalWeight + Data.Chance
end
local randomNumber = Random.new():NextNumber(0, totalWeight)
local weightSum = 0
for Rarity, Data in pairs(Database) do
weightSum = weightSum + Data.Chance
if randomNumber <= weightSum then
local RarityVal = math.round(Random.new():NextNumber(Database[Rarity].ValRange[1],Database[Rarity].ValRange[2]) * 100) / 100
return Rarity, RarityVal
end
end
end
I thought maybe I could multiply the chance value by the multi value on this line but it didn’t seem to work instead it made me get commons more often instead.
totalWeight = totalWeight + Data.Chance
Database:
local Data = {
["Common"] = {
Chance = 50; -- 1 in 2
ValRange = {1,99.99};
};
["Uncommon"] = {
Chance = 20; -- 1 in 5
ValRange = {100,299.99};
};
["Rare"] = {
Chance = 10; -- 1 in 10
ValRange = {300,499.99};
};
["Epic"] = {
Chance = 5; -- 1 in 20
ValRange = {500,749.99};
};
["Legendary"] = {
Chance = 0.1; -- 1 in 1000
ValRange = {750,1000};
};
["Mythic"] = {
Chance = 0.01;-- 1 in 10000
ValRange = {1000.01,1500};
};
};
return Data
Add up all the chances, generate a random number from 0 to that total, then pick one based on where it lands on a number line made up of segments whose lengths are the chances:
think of it more like a scale, where you weigh a type, if you increase one side, you’ll have to decide which side to decrease so that your LUCK system is fair and uniform. . . those changes are arbitrary and we can’t help much with it.
just an honest suggestion, stay true to maths, if you want to do x2, then keep this number in mind instead of doing like some RNG where x2 isn’t actually x2
What I tried rn is creating a variable for each rarity called luckboostable and if its true then I will multiply the chance for the rarity if its a high rarity
this code applies a flat luck multiplier to the total weight, rarer items will benefit more proportionally than the common ones.
-- Function to get a random item based on weighted chances and luck boost
function GetRandomItem(items, luckMultiplier)
-- Local variables
local totalWeight = 0
local randomValue
-- Calculate total weight of all items
for _, item in pairs(items) do
totalWeight = totalWeight + item.weight
end
-- Apply luck multiplier (optional)
if luckMultiplier then
totalWeight = totalWeight * luckMultiplier
end
-- Generate random value
randomValue = math.random(totalWeight)
-- Loop through items and check weight ranges
for _, item in pairs(items) do
local lowerBound = totalWeight - item.weight
totalWeight = lowerBound
if randomValue > lowerBound then
return item
end
end
-- If no item found (shouldn't happen), return nil
return nil
end
-- Example usage
local items = {
{ name = "CommonItem", weight = 10 },
{ name = "UncommonItem", weight = 5 },
{ name = "RareItem", weight = 1 },
}
local luckMultiplier = 1.5 -- Increase chance for rarer items
local randomItem = GetRandomItem(items, luckMultiplier)
if randomItem then
print("You got:", randomItem.name)
else
print("Error: No item found!")
end
it’s that, picture it as the amount of it you want on the total of all of the weigh…
like if your total weigh is 100 and your common weigh is 40 then ye it means 40%, but only if the total weigh isn’t limited to a specific number, so just try to tweak with it