this is the script:

local cost = 500

local petModule = require(game.ServerScriptService:WaitForChild("PetModule"))

script.Parent.ClickDetector.MouseClick:Connect(function(player)
	
	if player.leaderstats.Games.Value >= 500 then
		
		player.leaderstats.Games.Value = player.leaderstats.Games.Value - cost
		
	    local pet = petModule.chooseRandomPet()
	
	    print(pet.Name.." selected")
		
		game.ReplicatedStorage.HatchEgg:FireClient(player,pet)
		
	end
	
end)

this is the module script:

local petModule = {}

petModule.pets = {
   
   ["Legendary"] = {
       game.ReplicatedStorage.Pets.IPikachuRoblox;
   };   

   ["Common"] = {
      game.ReplicatedStorage.Pets["Baby lightning"];
   };

   ["Rare"] = {

   };
   
   ["Uncommon"] = {

   };

}

petModule.rarites = {
    
    ["Legendary"] = 2;

    ["Common"] = 50;

    ["Rare"] = 10;

    ["Uncommon"] = 25;
}

petModule.chooseRandomPet = function()
	
	local randomNumber = math.random(1, 100)
	
	
	local counter = 0
	
	for rarity, weight in pairs(petModule.rarites) do
		counter = counter + weight
		if randomNumber <= counter then
			
			local rarityTable = petModule.pets[rarity]
			local chosenPet = rarityTable[math.random(1,#rarityTable)]
			
			return chosenPet
			
		end
	end
	
end

return petModule

2 comments

0

Can you put your module script here too?

0

yes