Help with a table browse function

Hiya guys, i have a simple question today for you all.
I’m trying to code a way to browse through a table of items using a left/right button.
The current way i think i should do it is by making a general outside number and using the left or right button to add or subtract from it, then set the current item to whatever table listing has the same number.
Doing it this way, i feel like there’s some way i could make it much cleaner.
how would you go about doing this?

If you only need to go left and right in table of contents (like skyrim inventory menu, but horizontal instead).

Then all you need is to create a local and increment/decrement it dependant on the button/key pushed.
If you want to make it like an inventory (in form of grid, ex. minecraft inventory) then you need 2 locals (suggested: x, y)

In my opinion, a button to switch between items is a nuisance as you have to click it multiply times to get to where you want.
A much cleaner design in my eyes would be to either display all the items; so you could simply click on the one you want.
Or if you’d rather not display every item at once, Create some sort of slider to move between a 1D array of them.

If you are however stuck with using buttons to switch between them; the method you mentioned should be fine as long as you make sure to keep that number within the max #items.

edit: as a sub note, my response was from the point of view of a GUI button interaction and not a keyboard.

It’d be fairly simple:


local CurrentPage = 1

function MoveBack( )
  CurrentPage = CurrentPage - 1

  if CurrentPage < 1 then
    CurrentPage = MaxPages
  end
end

function MoveForward( )
  CurrentPage = CurrentPage + 1
  
  if CurrentPage > MaxPages then
    CurrentPage = 1
  end
end

Of course, that’s for displaying a single item. It can get more involved if you want to display a row of items as a page.

as it’s for a loadout system, there are multiple areas where you can select an item. I’m using a minimalist approach to it.