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
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.
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