local Ore_Chances = {
Coal = 0.8;
Copper = 0.5;
Gold = 0.25;
Diamond = 0.15;
}
local function OreRarity()
for name, chance in Ore_Chances do
local chosenRarity = (chance / 4) * 100
if chosenRarity == chance then
return chosenRarity
end
end
end
the math works how i want but wont work since i want to return the name but chosenrarity wont ever be equal to chance because chance isn’t divided/multiplied
This is all i tried and all posts about rarity aren’t very good imo so any help would be awesome
You want to use something called a “Weightage System”. Basically the key is the item and the value is it’s weight. The lower the weight the rarer it is. Then we will select a random number from 1 to the total of all weights combined. After that, we will make a var called counter to which we’ll be adding the weights to compare to the random nu,ber
local Ore_Chances = {
Coal = 0.8;
Copper = 0.5;
Gold = 0.25;
Diamond = 0.15;
}
local function OreRarity()
local randnum = Random.new(39473646824):NextNumber(0,1.7) -- 1.7 is total weight
local counter = 0
for name, weight in Ore_Chances do
counter += weight
if counter > randnum then
return name
end
end
end
This is a very solid RNG system which should be good for general usage
function getWeightedRandom(weightedList)
local totalWeight = 0
for name, v in weightedList do
totalWeight = totalWeight + v
end
local randomWeight = math.random() * totalWeight
local currentWeight = 0
for name, v in weightedList do
currentWeight = currentWeight + v
if randomWeight <= currentWeight then
return name
end
end
end
local items = {
Gold = 10,
Iron = 20,
Silver = 70,
}
local randomValue = getWeightedRandom(items)
print("Random value:", randomValue)