Help me with this 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 whole 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)[index]]
			
			return chosenPet
			
		end
	end
	
end

return petModule
2 Likes

It says that the second argument to math.random() is empty, so that means the table is empty or you did some mistake, can I see the line where you required the module script? (And at your module script if the problem isn’t in the requiring line)

1 Like

math.random requires 2 interval values, so for your index value for the rarityTable, add this:

local chosenPet = rarityTable[math.random(1, #rarityTable[index])  --Index, or your 'counter' letter in your for i,v table.

I will test this now, and see if my answer is correct, standby.

2 Likes

But doing #rarityTable would also work, because “#” would get how much stuff there is in that table.

EDIT: Thinking about it, your way could also be correct, but we both don‘t know how his module script looks like so we can‘t know it.

1 Like

Can I see the ModuleScript, I can’t help you until I see it.

1 Like