This is very hard to describe but basically im trying to make a randomization system, and when anything other than specific values print it errors
Error:
Function:
function Weight.Rarity_Roll(Random_Number)
for Rarity, Weight in pairs(Weight) do
if Random_Number <= Weight then
return print(Rarity, Random_Number)
end
end
end
Server Script:
game.Players.PlayerAdded:Connect(function(Player)
for i,v in pairs(script:GetChildren()) do
local Amount = {
v
}
Rarities.Rarity_Roll(math.random(1,100))
end
end)
The probability table Weight has a conflicting name with the module/other table Weight, so when you are defining
function Weight.Rarity_Roll()
you are creating a key “Rarity_Roll” with the value being a function in the probability table, which is messing up during iteration over the table, since you are then comparing the function value to a number, which throws an error.
Also the way your comparison is working seems clunky to me.
Seems it’ll always choose a number that is <= Weight unless Weight is over 80. So a random number of 1 is less than all the numbers you have as the Weight.
Seems you need checks like
if Random_Number <= 100 and Random_Number >=80 then
item = FlashLight
elseif Random_Number <80 and Random_Number >=70 then
item = Mining_Gloves
elseif --etc.
You should also take into consideration that dictionaries are unordered, meaning it will not be iterated over in the order you defined it and will mess up your current weighted probability system.
you could do something like this instead:
local Weight = {
--Common Accessories--
FlashLight = 80,
Mining_Gloves = 70,
Mining_Helmet = 65,
--Rare Accessories--
Reinforced_Helmet = 40,
Improved_Gloves = 35,
Infared_Flashlight = 25,
--Epic Accessoires--
Evolved_Helmet = 15,
Ultraviolet_Flashlight = 10,
--Legendary Accessories--
Godly_Helmet = 5,
Corrupted_Mining_Gloves = 1
}
local totalValue = 0
for _, v in pairs(Weight) do
totalValue+=v
end
function Weight.Rarity_Roll(Random_Number)
local cVal = math.random(1, totalValue)
for Rarity, Weight in pairs(Weight) do
cVal -= Weight
if cVal <= 0 then
return print(Rarity, Random_Number)
end
end
end