local cost = 1000

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

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

Error: Workspace.lil' demon egg.Text.Script:11: attempt to index nil with 'chooseRandomPet'

Edits (module script):

local petModule = {}

petModule.pets = {

	["Legendary"] = {
		game.ReplicatedStorage.Pets["lil' demon"]
	};
	
	["Rare"] = {
	
	};
	
	["Uncommon"] = {
	
	};
	
	["Common"] = {
	
	};
	
}

-- Selection Chance 100
petModule.rarities = {

	["Legendary"] = 5; -- 5% chance

	["Rare"] = 15; -- 15% chance
	
	["Uncommon"] = 30; -- 30% chance
	
	["Common"] = 50; -- 50% chance
}

petModule.chooseRandomPet = function()
	
	local randomNumber = math.random(1, 100)
	
	local counter = 0
	
	for rarity, weight in pairs(petModule.rarities)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 module

5 comments

0

Can you show the snippet of the your module where chooseRandomPet is defined? Are you making sure to use "return" to give something back?

0

Yozoh I made edits.

0

Sorry, I can't decipher what you're trying to do in petModule.chooseRandomPet. But make sure line 55, you return petModule and not module.

0

I'll bump this in hopes this gain more traction. Please reach out on the devforums or the scripting helpers discord for a more active community that will be able to answer your question faster & better.

0

I don’t see a variable for (choose random pet) on the first code listed. What do you mean by (choose random pet?)