# How do i add luck boosts into pet system?

so i wanted to add luck boosts into my game with pet system (i dont have pet system yet but i know how to do one) i wanted to use this type of picking pet

function ChoosePet(Egg)

``````	local Data = Eggs[Egg]
local Pets = Data["Pets"]
local TotalWeight = 0
for i,v in pairs(Pets) do
TotalWeight = TotalWeight + v.Rarity
end
local Chance = math.random(1,TotalWeight)
local Counter = 0
for i,v in pairs(Pets) do
Counter = Counter+v.Rarity
if Counter >= Chance then
return v.Name
end
end
end
``````

Pet rarity data would be in module script
i need it to work with diffrent types of luck, so all i have to do is tell script how much % more they have for specific pets

i think the best way of doing that i telling script how much % more they have for good ones and removing same amount you added to good pets from bad pets, but iâ€™m not sure if this will work.

When randomizing you can increase the number in the first argument of the random function which is currently 1 to a higher number like 2 for a better chance to get things in between the range. You could also create a bool value for the player. The value of that bool would be true or false based on if you have that specific boost enabled. Then in your script you can simply check if the bool value is true (means that they have that specific boost on) and if it is then you can change the 1 to 3 or 2 maybe if itâ€™s not true then you can just make it 1 again.

Look at this solution I posted on somebody elses topic, I think it will help you: Problem with table chances randomizer - #25 by bluebxrrybot

Heâ€™s trying to make a random ore given when you mine ore based on chances, and youâ€™re trying to give a random pet when you open an egg based on chances, so same thing really.

1 Like

its very hard to understand but i will read it in a moment

what if randomizer would pick a number that is not assigned to anything?

Um. The randomizer will pick a random number in the specified range. The second argument defines the range of the function not the first so if anything were eliminating worse pets. If you want to make the randomizer boost more random you can even randomize to decide if you wanna apply the luck boost to the randomizer. Which I recommend you do.

This might not be scalable with multiple eggs that well, so there would be a lot of memory used for each egg I think.

``````local Egg = {
Dog = 0.3,
Cat = 0.3,
Parrot = 0.175,
Duck = 0.175,
Goose = 0.05,
}

-- Convert Egg to an array to be sorted

local pets = {}

for i, v in pairs(Egg) do
table.insert(pets,{i,v})
end
table.sort(pets, function(a, b)
return a[2] < b[2]
end)

-- Sort Egg into segments
local segments = {}
local total = 0
local luckboost = 1 -- increase value to increase luck
for _, data in pairs(pets) do
table.insert(segments, {total, total + data[2], data[1]})
total = total + data[2]
end

local depth = 10000 -- more depth allows for more preciseness for chance, but takes longer to compute
-- 1000 depth allows for chances like: 0.0001.

local function HatchEgg()
local x = math.random(0,depth) / depth
x = x / luckboost -- forces x to be closer to smaller segments
for _, seg in pairs(segments) do
if seg[1] <= x and x <= seg[2] then
return seg[3]
end
end
return "Unknown"
end

for i = 1, 10 do
print(HatchEgg())
end
``````
2 Likes

A value of 1 for the chance is 100%. Gooseâ€™s chance is 0.05, this shows how rare it is:

Compaired to the dog:

will this â€śluck boostâ€ť value work for any number? up to 5?

Yes, increasing the luck booster will decrease the chance of getting more common pets, and increase the chance of getting rarer pets. If you increase the luck to 100 times, itâ€™s impossible to get a dog or cat.

i will make this as done but i havenâ€™t tried it yet so i will let you know if it works for me when iâ€™m done with this. and donâ€™t worry about mememory, it canâ€™t be that bad and i dont have much eggs in game yet

If you still want players to have a chance to get a common pet you can also randomize to see if the luck boost should apply. Pretty much randomize and if it picks that specific number it will apply the luck boost but if it picks the other number then it wont apply the luck boost. This will make you still lucky and it will add some diversity to the pets you get instead of getting a rare pet everytime.

thats a good idea too, i will keep it in mind while making my pet system because i dont want players to have too over power pets just for paying money in game (p2w)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.