Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Line 31 Attempt to get length of a nil value?

So I have a problem with my module script where is says there is a nil value I have tried going on the dev forum and looking it up, and just simply looking up the error but here is the script

P.S this is for a egg system

local petModule = {}

petModule.pets = {
	["Epic"] = {game.ReplicatedStorage.pets.LavaBird};
	["Rare"] = {game.ReplicatedStorage.pets.StereoPet};
	["Uncommon"] = {game.ReplicatedStorage.pets.DodgeBall};
	["Basic"] = {game.ReplicatedStorage.pets.BasicDoggo};
	
}

petModule.rarityTable = {
	
	["Epic"] = 4;
	["Rare"] = 16;
	["Uncommon"] = 30;
	["Common"] = 50;
	
}

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

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

I think the problem is that there is nothing called "Rarity" In Petmodule.pets, I might be wrong.

You have a naming mismatch. Your code will error on cases where the chosen Rarity is "Common", when in reality you're looking for "Basic." Try changing line 16's ["Common"] to line 07's "Basic", or vice versa.

This discussion is preserved as it was. New answers, comments, and votes are closed.