Hey not sure if this helps, but here’s how I make my Randomizers.
function(chances: {}, luck: number)
local luck = luck or 1
local chances = table.clone(chances)
--Lets get total chance
local totalChance: number = 0
for _, chance in ipairs(chances) do
totalChance += chance
end
--Lets apply luck
for i, chance in ipairs(chances) do
if chance / totalChance <= .1 then
chances[i] *= luck
end
end
--Lets scale chances into relative weights
local n: number = 0
local totalWeight: number = 0
local options = {}
local relativeWeights = {}
--Lets get total weight and seperate options and relative weights
for option, relativeWeight in ipairs(chances) do
n += 1
totalWeight += relativeWeight
options[n] = option
relativeWeights[n] = relativeWeight
end
--Time calculate relative weights
for i = 1, n do
relativeWeights[i] /= totalWeight
end
--Time to choose
local choosenNum: number = randomizer:NextNumber()
for i = 1, n do
choosenNum -= relativeWeights[i]
if choosenNum < 0 then
return options[i]
end
end
end
function module.getRandomPet(player : Player, pets : {})
--Time to format pets table, it will help us rarity such as same for example if we have two 35 rarity we make them one so it makes it equal
--since those both pets has same chances
local chances = {}
for i, pet in ipairs(pets) do
chances[i] = pet.Rarity
end
--Luck
local ownsLucky = MarketPlaceService:UserOwnsGamePassAsync(player.UserId,GameSettings.PassesId.Lucky) or player:WaitForChild("Passes"):FindFirstChild('Lucky') or player:WaitForChild("Boosts"):FindFirstChild('Lucky')
local ownsSuperLucky = false
local luck = 1.25 --starts with 25% luck
if ownsLucky == true then --50% better luck
luck += .5
end
if ownsSuperLucky == true then -- 100% better luck
luck += 1
end
if player:GetAttribute('VIP') == true then -- 15% better luck for vip
luck += .15
end
local roll = module.ChanceRoll(chances, luck)
local choosenPet = pets[roll].PetId
return choosenPet
end
How do I have the Pets Saved?
['Pets'] = {
[1] = {PetId = 'Noob', Rarity = 50},
[2] = {PetId = 'Fox', Rarity = 35},
[3] = {PetId = 'Monkey', Rarity = 14},
[4] = {PetId = 'Flamingo', Rarity = 0.99},
[5] = {PetId = 'Parrot', Rarity = 0.01},
[6] = {PetId = 'Dragon', Rarity = 0.001},
}