Pet system randomizer

I have this half of a script, but its not randomizing it. It will print say number 2, but choose the wrong category.

petModule.rarities = {
	["Legendary"] = 5;
	["Rare"] = 15;
	["Uncommon"] = 30;
	["Common"] = 50;
}


petModule.chooseRandomPet = function()

	local randomNumber = math.random(1,100)
	print(randomNumber.. " choosen")
	local counter = 0

	for rarity, weight in pairs(petModule.rarities) do
		counter = counter + weight
		if randomNumber <= counter then	

			local rarityTabel = petModule.pets[rarity]
			local chosenPet = rarityTabel[math.random(1,#rarityTabel)]

			return chosenPet

		end
	end

end


return petModule
2 Likes

It’s hard to tell because you aren’t showing your pets table, however it may be because you can’t get the length of a table whose keys are strings.

Here’s what I would do personally:

local petModule = {}

petModule.Rarities = {
    Legendary = {
        ['LowBounds'] = 1;
        ['UpperBounds'] = 5;
    };
   Rare = {
        ['LowBounds'] = 6;
        ['UpperBounds'] = 20;
    };
    Uncommon = {
        ['LowBounds'] = 21;
        ['UpperBounds'] = 50;
    };
    Common = {
        ['LowBounds'] = 51;
        ['UpperBounds'] = 100;
    };
};

petModule.Pets = {
    ['Common'] = {'Pet1'; 'Pet2'; 'Pet3'}; -- example
    ['Uncommon'] = {'Pet4'; 'Pet5'; 'Pet6'};
    ['Rare'] = {'Pet7'; 'Pet8'; 'Pet9';};
    ['Legendary'] = {'Pet10'; 'Pet11'; 'Pet12'};
};

function petModule.chooseRandomPet()
    local randomNumber = math.random(1, 100)
    local rarity
    for index, table in pairs(petModule.Rarities) do
        if randomNumber >= table['LowBounds'] and randomNumber <= table['UpperBounds'] then
            rarity = index
        end
    end
    return petModule.Pets[rarity][math.random(1, #petModule.Pets[rarity])]
end

print(petModule.chooseRandomPet()) -- prints the pet
1 Like

Sorry here is this

local petModule = {}

petModule.pets = {

	["Legendary"] = {
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Angelerfish");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Ultimus");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("LavaLord");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("DemonicDominus");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("lilfly");
	};

	["Rare"] = {
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Angel");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("GoldenCat");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Jellyfish");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("LightBat");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("MythicalDemon");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Shark");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Snowman");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Starry");
	};

	["Uncommon"] = {
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Builder");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Robot");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Fedora");
	};

	["Common"] = {
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Cat");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Bear");
	};
}

Or the full thing.

local petModule = {}

petModule.pets = {

	["Legendary"] = {
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Angelerfish");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Ultimus");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("LavaLord");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("DemonicDominus");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("lilfly");
	};

	["Rare"] = {
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Angel");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("GoldenCat");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Jellyfish");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("LightBat");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("MythicalDemon");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Shark");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Snowman");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Starry");
	};

	["Uncommon"] = {
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Builder");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Robot");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Fedora");
	};

	["Common"] = {
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Cat");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Bear");
	};
}
petModule.rarities = {
	["Legendary"] = 5;
	["Rare"] = 15;
	["Uncommon"] = 30;
	["Common"] = 50;
}


petModule.chooseRandomPet = function()

	local randomNumber = math.random(1,100)
	print(randomNumber.. " choosen")
	local counter = 0

	for rarity, weight in pairs(petModule.rarities) do
		counter = counter + weight
		if randomNumber <= counter then	

			local rarityTabel = petModule.pets[rarity]
			local chosenPet = rarityTabel[math.random(1,#rarityTabel)]

			return chosenPet

		end
	end

end


return petModule