It looks like the issue here is with reading from a dictionary.
It looks like petsModule.pets is a dictionary of pets, and "rarity" is the name of your iterator in a generic for loop. Your generic for loop iterates over petModule.rarities (a dictionary with four values) meaning that it should be running the code of the for loop up to four times. In that loop, you have line 45:
local rarityTable = petModule.pets[rarity]
What this appears to be doing is trying to set rarityTable to the first item of petModule.pets, because "rarity" is the iterator number. This should mean that the value of "rarityTable" would presumably be set to "Legendary" (or something else, because you can't exactly iterate over a dictionary in a set order using ipairs, so you can expect a different result every time). "Legendary" is the name of a key - a key to a table in petModule.pets.
I wrote the following code to try to bugtest it:
print("Rarity selected: " .. rarity);
print(petModule.pets[rarity]);
print("Size: " .. #petModule.pets[rarity]);
print("Contained in the table:")
for i,v in pairs(petModule.pets[rarity]) do
print(v);
end
print(petModule.pets[rarity[math.random(1,#petModule.pets[rarity])]]);
and received the following output:
Rarity selected: Common
table: 0xf9b27cdd0777af87
Size: 1
Contained in the table:
Baby lightning
nil
The problem seems to be that you're unable to get an item from a table contained in a dictionary by indexing the rarity table directly.
There are workarounds and alternate methods you could use. For example, you could always do this to select a random index:
local size = #petModule.pets[rarity];
local randomNumber = math.random(1,size);
for i,v in pairs(petModule.pets[rarity]) do
if i == randomNumber then
print("Randomly selected");
print(v);
end
end
which prints the following when Common is the selected rarity:
Randomly selected
Baby lightning
This should fix your problem, but this is personally not the method I would use because I likely would've structured the script differently from the start. I tried to solve it without making much change to your code.
I hope this helps! If it did, please mark it as the solution or upvote it, but if it doesn't, please leave a comment below if you have any further questions.
1 comment
thanks!