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

why the output error says: ServerScriptService.PetModule:46: bad argument #2 (interval is empty)?

why? this is the script:

local petModule = {}

petModule.pets = {
   
   ["Legendary"] = {
       game.ReplicatedStorage.Pets.IPikachuRoblox;
   };   

   ["Common"] = {
      game.ReplicatedStorage.Pets["Baby lightning"];
   };

   ["Rare"] = {

   };
   
   ["Uncommon"] = {

   };

}

petModule.rarites = {
    
    ["Legendary"] = 2;

    ["Common"] = 50;

    ["Rare"] = 10;

    ["Uncommon"] = 25;
}

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

1 answer

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); -- should be a key
print(petModule.pets[rarity]); -- should output a table
print("Size: " .. #petModule.pets[rarity]); -- should be the number of items in the table
print("Contained in the table:")
for i,v in pairs(petModule.pets[rarity]) do
	print(v); -- should be the name of an item in the table
end
print(petModule.pets[rarity[math.random(1,#petModule.pets[rarity])]]); -- if all goes well, this should be the name of something in the table, too

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]; -- Maximum
local randomNumber = math.random(1,size); -- Select a random number
for i,v in pairs(petModule.pets[rarity]) do -- Iterate over the table
	if i == randomNumber then -- If index number i matches the random number
		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

0

thanks!

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