local Chance = math.random(1,100)
local Counter = 0
for _,pet in pairs(game.ReplicatedStorage.Pets[egg.Name]:GetChildren()) do
Counter = Counter + pet.Config.Rarity.Value
if Chance <= Counter then
return pet.Name
end
end
end
heres my egg hatching rarity system, im wanting to add a luck variable into this so that the player has a higher chance to get a rarer pet does anybody know how i could do this?
I suppose you’ve seen this loop from a guide or tutorial somewhere?
Either way, a quick solution could be the following:
local ExtraLuck = 10
local Chance = math.random(1,100)
Chance -= ExtraLuck -- Since a lower Chance equals a better rarity we substract instead of adding.
local Counter = 0
for _,pet in pairs(game.ReplicatedStorage.Pets[egg.Name]:GetChildren()) do
Counter = Counter + pet.Config.Rarity.Value
if Chance <= Counter then
return pet.Name
end
end
Personally I’ve never understood the principal of that type of for-loop. Because if the for-loop fetches one of the most common pets first, then it adds that rarity to the counter, and the chance is automatically below that.
I would personally make a Dictionary, where all the data like Chance is stated, that way you can also use ipairs
-- In a PetsData modulescript perhaps
local PetsData = {
["NormalEgg"] = {
[1] = {PetName="MostRare",Chance=0.1},
[2] = {PetName="AlmostAsRare",Chance=0.12},
[3] = {PetName="AlmostCommon",Chance=25},
[4] = {PetName="VeryCommon",Chance=66},
[5] = {PetName="IfAnythingElseFails",Chance=100},
},
}
-- Inside function
local EggType = "NormalEgg"
local Chance = math.random(1,10000)/100 -- This does so we get decimal chances aswell
Chance -= ExtraLuck -- Since a lower Chance equals a better rarity we substract instead of adding.
for _,pet in ipairs(PetsDataModuleScript.PetsData[EggType]) do
if Chance > PetsDataModuleScript.PetsData[EggType].Chance then continue end -- Continues to next in the loop, if our chance value was higher than the rarity chance
return pet.Name
end
Just be aware that this in theory wouldn’t be %-of getting a pet, since the combined chance is 191.22. But you can always just leave it like is, or go through all the eggs with a for-loop within the modulescript, and calculate what their percentage would be out of a 100.
That’s not actually true, for anything to occur there is always a percent chance.
In this case, the chance of an item occurring is: its rarity minus the rarity of the item that’s closest to its rarity but slightly rarer and that whole divided by the highest rarity number you have(always 100 in ur case) then to get the percentage you multiply by 100.
In the case of the rarest item, the number is its rarity, of course divided by the max number you have and multiply by 100.
So the ifAnythingElseFails rarity must be at 100 to guarantee an item will be given. And its rarity is ((100-33)/100)*100 = 34%.
I know the divide and multiply by 100 makes little sense, but just imagine if you wanted a rarity out of 10,000. Now you need it.
That’s a pretty rude thing to say, I was just making sure the person fully understood the solution. You didn’t have to come back with a sarcastic response, and anyway I don’t even celebrate Christmas.
I did not have any ill intentions with my greeting, but to the topic.
So if I’ve understood you right. We have the following 3 values:
local MostCommon = 100
local VeryRare = 0.5
local ExtremelyRare = 0.4
There is 0.5% chance of getting VeryRare?
Because in my head it will look like this:
local MostCommon = 100
local VeryRare = 0.5 -- 0.1% chance of getting this, since anything above will go to MostCommon, and from 0.4 and lower will go to ExtremelyRare.
local ExtremelyRare = 0.4
In this case the rarity of veryRare would be ((.5-.4)/100*100). So .1% is correct.
We take the rarity of the item we are talking about and subtract the rarity of the closest item that is rarer than it. So (VeryRare - ExtremelyRare).
Then we divide by the highest number we calculate in our math.random() function, which I will assume to be 100.