Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

[FOUND SOLUTION] Pet chances not supporting percentages lower than 1?

Asked by
Gogulsky 129
4 years ago
Edited 3 years ago

This has been a problem for many people including me. The local Chance doesn't make the pets under 1% hatchable. The 1 in math.random(1,TotalWeight) would need to be 0 but it won't work.

01function ChoosePet(Egg)
02    local Data = Eggs[Egg]
03    local Pets = Data["Pets"]
04    local TotalWeight = 0
05    for i,v in pairs(Pets) do
06        TotalWeight = TotalWeight + v.Rarity
07    end
08    local Chance = math.random(1,TotalWeight)
09    local Counter = 0
10    for i,v in pairs(Pets) do
11        Counter = Counter+v.Rarity
12        if Counter >= Chance then
13            return v.Name
14        end
15    end
16end

This script is from the better pet system. However there's another pet system that has working pet chances, it basically has a math.random(1,TotalWeight) too however it also has this:

1if Chance <= Counter then
2                return i

Both of these pet systems obviously work differently. The "i" are the suffixes and I believe that this is what makes the pets under 1% hatchable. I still don't know how to make this possible in the other pet system. Any help?

0
I think it is because math.random does not go into decimals by itself, the way I get math.random with decimals is do something like math.random(0,100)/100 munkuush 22 — 4y
0
Looking at this, its half way down, math.random returns a decimal value between 0 and 1, multiply this by some value and this will give you a value between 0 and some value. This should fit you case. https://developer.roblox.com/en-us/api-reference/lua-docs/math marine5575 359 — 4y
0
A use Case: math.randomseed(math.random()*math.random) local Chance = math.random() * 100 if chance <= counter then return v.Name marine5575 359 — 4y
0
Please remember to use math.randomseed so that your pet system is not susceptible to RNG manipulation! marine5575 359 — 4y

Answer this question