Okay so, I’m currently making an egg hatching system and I’m stuck on the part when I need to get a random pet, this pet system is working on percentages
here is the rarity table for the first egg
Rarities = {
{"Dog", 50};
{"Cat", 30};
{"Bunny", 20};
{"Mouse", 0.1};
}
I think i’ve done something wrong, I’m not sure if this system is the best, how can I improve it?
local function GetRealLength(LowestPercentage)
local TempLow = LowestPercentage
local MultiTimes = 0
while TempLow < 0.1 do
MultiTimes += 1
TempLow *= 10
end
MultiTimes += 1
local RealLength = (tostring(TempLow):len() - 2) * (MultiTimes)
LowestPercentage = string.format("%."..tostring(RealLength).."f",LowestPercentage)
return (RealLength > 0 and RealLength or 1) , 100 * (10^RealLength), LowestPercentage
end
local function GetRandomPet(EggName)
local Rarities = GetRarities(EggName)
table.sort(Rarities, function(a, b)
return a[2] < b[2]
end)
local LowestPercentage = Rarities[1][2]
local MaxNumber = 100
local RealLength = 1
if LowestPercentage < 1 then
RealLength, MaxNumber = GetRealLength(LowestPercentage)
end
local RandomNumber = RNG:NextInteger(1,MaxNumber)
RandomNumber /= (10^RealLength)
print(RandomNumber)
for i,v in ipairs(Rarities) do
if RandomNumber <= v[2] then
print(v[1])
break
end
RandomNumber -= v[2]
end
end
If you find any problem/Things I can do to improve, please tell me.
Thanks