This is an odd issue. I tried playing around with this in Studio and I got some unexpected results, so I threw in some code to try and diagnose the problem. Specifically, I added a for loop (pairs) to see what was happening.

```
Tails = {
[1] = {Cost = 0},
[2] = {Cost = 250},
[3] = {Cost = 500},
[4] = {Cost = 1000},
[5] = {Cost = 2500},
[6] = {Cost = 5000},
}
local PlayersTail = 1
local NextTail, TailData = next(Tails, PlayersTail)
print(PlayersTail, NextTail, TailData)
for i,v in pairs(Tails) do
print(i, v)
end
```

For whatever reason, your table’s order is as follows: 6, 2, 3, 1, 4, 5. When using ipairs however, since you’re using numeric keys in your Tails table, it comes out as expected: 1-6.

I don’t know what your code looks like right now but it seems that you’ve decided to switch over to numeric keys. If you’re keeping it that way, good. Let’s do this: instead of using the `next`

function, assume that whatever numbers you have are your keys. Then to get the next value, just add.

```
Tails = {
[1] = {Cost = 0},
[2] = {Cost = 250},
[3] = {Cost = 500},
[4] = {Cost = 1000},
[5] = {Cost = 2500},
[6] = {Cost = 5000},
}
local PlayersTail = 1
local NextTail = PlayersTail + 1
local TailData = Tails[NextTail]
print(PlayersTail, NextTail, TailData)
```

Then, if TailData is nil, you know that there’s no value for the indice Tails[NextTail]. In addition, because you have numeric indices, you can use the length (#) operator to get the table size, if you ever need it. Printing out `#Tails`

gives 6. Then you could do… I don’t know, whatever math you need.

```
local NextTail = math.max(#Tails, PlayersTail + 1) -- Clamps to a max
```