Help me with the Script please!

  1. What do you want to achieve? I want to fix it
  2. What is the issue? CaptureFORUM3
  3. What solutions have you tried so far? I tried to edit the script.

The Part Of The Script: ( Is not the full one)

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

The Module Script:

local petModule = {}

petModule.pets = {
	
	["Mythic"] = {
		
	};
	
	["Legendary"] = {
		
	};
	
	["Epic"] = {
		
	};
	
	["UnCommon"] = {
		game.ReplicatedStorage.Pets.BlackCaterpillar;
		game.ReplicatedStorage.Pets.WhiteCaterpillar;
	};
	
	["Common"] = {
		game.ReplicatedStorage.Pets.GreenCaterpillar;
		game.ReplicatedStorage.Pets.DarkGreenCaterpillar;
		game.ReplicatedStorage.Pets.LightGreenCaterpillar;
	};

}

petModule.rarities = {
	
	["Mythic"] = 1;
	
	["Legendary"] = 3;
	
	["Epic"] = 17;
	
	["UnCommon"] = 24;
	
	["Common"] = 55;
	
}

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)[#rarityTable]]
			
			return chosenPet
			
		end
	end
	
end

return petModule

Can you print #rarityTable and see what it prints? I’d assume the problem is that #rarityTable is 0 so you’re trying to do something like

math.random(1,0)

There is insufficient information. What do you want your script to achieve? Not what u want to do with your script. What have u tried to fix the script, what did u edit. Not what u want to do with your script.

At the script u did rarityTable[math.random(1,#rarityTable)[#rarityTable]]. U are trying to index the random nubmer.

Three empty tables should explain it exactly why it is 0. Try to check if the table is empty or not. If it’s not empty, continue.

1 Like