For some reason its not properly randomizing it doesnt even pick fire it barely picks fire its always acid or water but its always acid, even though they are the same
local partscanrandomspawn = {
fire = 35,
water = 35,
acid = 35,
}
function PickRarity()
local Index = math.random(1, 100)
local HighestRarity = nil
for RarityName, Value in pairs(partscanrandomspawn) do
if HighestRarity == nil then
HighestRarity = RarityName
print(RarityName)
elseif Index <= Value and Value >= partscanrandomspawn[HighestRarity] then
HighestRarity = RarityName
print(RarityName)
elseif Value == partscanrandomspawn[HighestRarity] then
print(RarityName)
local a = math.random(1,2)
if a == 1 then
HighestRarity = RarityName
end
end
end
return HighestRarity
end
There are a few issues, first the index doesn’t cover all numbers (35 + 35 + 35 = 105). Second you basically just take whatever comes first as the Highest Rarity, once you do that you then go to the next object in the table, and in order for that to be chosen the Index (which is a random number between 1-100) has to be 35 or higher, which then changes the rarity to that, but if that is true, then that rarity is always going to get overwritten and then chosen by the last rarity.
To fix this I would completely rewrite it and do something like this:
local partsCanRandomSpawn = {
fire = 35,
water = 35,
acid = 35,
}
local function pickRarity()
local total = 0
for _, v in partsCanRandomSpawn do
total += v
end
local randomNum = math.random(total)
total = 0
for rarityName, rarityValue in partsCanRandomSpawn do
total += rarityValue
if randomNum <= total then
return rarityName
end
end
end