ModuleScript attempt to index nil with 'Common'

im making an egg hatching system and i sent arguments to a for loop on the modulescript and it prints out the error "attempt to index nil with ‘Common’ "
there is probably a solution to this but im too stupid to figure it out

heres a part of the modulescript

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

and here is the script that sends the argument

player:WaitForChild("Data"):WaitForChild("Coins").Value -= cost
		
local pet = petModule.chooseRandomPet("CommonEgg")
		
game.ReplicatedStorage:WaitForChild("HatchEgg"):FireClient(player,pet)

Most likely this does not exist.

petModule.pets.test

You might mean

petModule.pets[test][rarity]

But other than that I can’t think of anything.

You can’t index a table using the dot operator with a string value.

Both versions translate to the following:

petModule.pets."test"[rarity] --Syntax error.
petModule.pets["test"][rarity] --Valid syntax.
1 Like

The reason why it’s an error of existence is whether intentional or not they were looking up the wrong thing since the following 2 lines are identical.

petModule.pets.test
petModule.pets[“test”]

They likely meant to use the string in test as reference

petModule.pets[test]

It could be considered a syntax error since they unintentionally used the wrong syntax. But it is worth noting that the error generated is related to existence not syntax specifically.

But other than that I can’t think of anything.

Your original comment seemed uncertain so I decided to clear up the reason.

In either case you can’t index a table with a string literal or a variable which points/refers to a string literal with the dot operator.

1 Like

Ignore this post, didn’t mean to make a second one.