Sorting numbered key-table pairs?

Hi, so I’m finishing up on a fishing system, and apparently, no matter how I arrange it, the fish chance key-tables I’m creating just do not want to iterate correctly. It’s always the one certain value that’s always the first to get iterated on. This completely ruins my system. A little background as to what I’m doing:

I have a system set up similar to this:

 local fish = {
      [25] = {name = "Goldfish"};
      [100] = {name = "Common Goldfish"};

  }

Basically, each time the player fishes, a random number is drawn from 1-100. Then, a loop iterates through the fish stats for whatever fishing rod the player is using, and determines which fish has been caught. Why it’s important that the loop iterates through these keys smallest to largest, is because I check to see if the number drawn is smaller or equal to the chance of the fish. So like, an example of the problem, if the number drawn is 20, technically it should get the rare Goldfish. But, if the loop iterates through the 100 value first, it’ll select the common goldfish.

Thanks for reading!

1 Like

Unfortunately dictionaries do not support ordering, only regular arrays are ordered.

I’d suggest doing something like this:

local fish = {
    { chance = 25, name = "Goldfish" },
    { chance = 100, name = "Common Goldish" }
}

Hopefully this solves your issue! :slight_smile:

1 Like

I sort of figured that :sob:

Thanks for your reply! :slight_smile:

2 Likes