I'm trying to add elements to the NPC Trade, so when the "luaOpenShopWindow" method is triggered I'm sending a list of IDs and Quantities, something like this:
{"id":2160,"buy":5000,"sell":0,"requiredItems":[{"count":10,"id":2152}],"subType":1,"name":"crystal coin"}
{"id":2520,"buy":1,"sell":0,"requiredItems":[{"count":1,"id":7618}],"subType":1,"name":"demon shield"}
This is my adapted code:

For the first item it gets the values normally and populates the variable correctly.
Now in the second item that it goes through, whether or not there is a second item in this "requiredItems" it gives the error "invalid key to 'next'"
Can anyone help me?
{"id":2160,"buy":5000,"sell":0,"requiredItems":[{"count":10,"id":2152}],"subType":1,"name":"crystal coin"}
{"id":2520,"buy":1,"sell":0,"requiredItems":[{"count":1,"id":7618}],"subType":1,"name":"demon shield"}
This is my adapted code:
C++:
lua_getfield(L, tableIndex, "requiredItems");
if (lua_istable(L, -1)) {
std::list<ShopRequiredItem> requiredItemsList;
lua_pushnil(L);
while (lua_next(L, -2) != 0) {
const auto tableIndexReqItem = lua_gettop(L);
if (lua_istable(L, tableIndexReqItem)) {
ShopRequiredItem reqItem;
reqItem.itemId = tfs::lua::getField<uint32_t>(L, tableIndexReqItem, "id");
reqItem.count = tfs::lua::getField<int32_t>(L, tableIndexReqItem, "count");
requiredItemsList.push_back(reqItem);
}
lua_pop(L, 1);
}
item.requiredItems = requiredItemsList;
}

For the first item it gets the values normally and populates the variable correctly.
Now in the second item that it goes through, whether or not there is a second item in this "requiredItems" it gives the error "invalid key to 'next'"
Can anyone help me?