Unknown Issue With Module

I posted something similar yesterday, no one replied so I figured I could try something simpler.
Theres a function in this module script that chooses a random item from each subtable in the table “petModule.pets”. However, whenever it tries to pick any item other than the ones in the first/last subtable it says: attempt to index nil with ‘Clone’ ,the error is from another script but thats just getting the function and pet table so there can’t be any errors on that script.


local petModule = {}
local petFolder = game.ReplicatedStorage.Pets
petModule.pets = {

	["(rarity)"] = {
		petFolder["(pet)"];
	};

	["(rarity)"] = {
		petFolder["(pet)"];
	};

	["(rarity)"] = {
		petFolder["(pet)"];
	};

	["(rarity)"] = {
		petFolder["(pet)"];
		petFolder["(pet)"];
		petFolder["(pet)"];
		petFolder["(pet)"]; -- (pet) and (rarity) is just me replacing the original values--
	};
	["(the only rarity that works)"] = { -- this is the only rarity that will return the items--
		petFolder["(pet)"];
		petFolder["(pet)"];
	};
}

petModule.rarities = {
	["(rarity)"] = 5;

	["(rarity)"] = 5;

	["(rarity)"] = 10;

	["(rarity)"] = 25;

	["(rarity)"] = 55;
};

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

thanks!

Count is being reset every loop. Switch these two lines.

1 Like

Now it gives me the error: Module code did not return exactly one value

You seem to have a duplicate method that’s the same as the one at the bottom.

Was that a typo? If not, that’ll throw an error as first off you can only return one value from a module, and a second thing is that after your return statement (petModule should be nil as well since it’s a local field below that statement) you have another block of code which will not run due to the return statement being above it, so that’ll throw a compilation error as well.

2 Likes

If that were the case, it likely wouldn’t even run since petModule is nil at the very top of the script.

@Axyndey can you please share the script again as it currently exists?

2 Likes

Oh I didn’t notice that lol! Thats actually a typo on my part, but not in the code itself, just this post, thanks for telling me.

Here it is, I have replaced the rarities and pets with placeholders because I don’t really want to reveal what that info is, I don’t know why, its just a weird thing with me.

local petModule = {}
local petFolder = game.ReplicatedStorage.Pets
petModule.pets = {
	
	["()"] = {
		petFolder["()"];
	};
	
	["()"] = {
		petFolder["()"];
	};
	
	["()"] = {
		petFolder.();
	};
	
	["()"] = {
		petFolder["()"];
		petFolder["()"];
		petFolder["()"];
		petFolder["()"];
	};
	["()"] = {
		petFolder["()"];
		petFolder["()"];
	};
}

petModule.rarities = {
	["()"] = 5;
	
	["()"] = 5;
	
	["()"] = 10;
	
	["()"] = 25;
	
	["()"] = 55;
};

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

@JarodOfOrbiter

If that’s the full script, you forgot return petModule at the bottom.

1 Like

I’m stupid XD. Anyway, thanks so much for your help! This has been so annoying and I’m so glad someone helped me fix it.