How to use an index with /execute with Vanilla Minecraft Commands

Solution 1:

I think I can see two ways to do this. One is more simple but does not give you exactly what you want, whereas the other is more complex but I think it does do what you want.

The simple way is to give every player a unique id on login, and have that id displayed next to each players' name in tablist (using scoreboard setdisplay sidebar). Then, you teleport to a player using trigger objective set [ID of person you want to TP to] and then the command logic should be pretty straightforward from there.

The second method is to assign these IDs on the fly. When a player runs the command to open the menu, you assign the nearest 10 players a unique TPA-ID-score between 1-10. Then, you have a mcfunction with 10 of your trigger commands hardcorded 1-10, where you do trigger objective set 1, trigger objective set 2 and so on. (you would also change the selector to make sure the numbers line up). If you want an additional "page" of results, give all players with TPA-ID-score 1-10 some sort of tpa-excluded tag, and assign the next 10 players a unique TPA-ID-score 1-10. Then print out your 10 tellraw messages again.

This will allow you to scroll through every player on the server, and when someone chooses to click on someone to teleport to, reset by removing everyone's tpa-exclude tag and set all TPA-ID-scores back to zero.

The downside of this second method is that multiple players would not be able to use it at the same time (you'd have to prevent players from opening the tp menu while another player is using it), whereas with the first method is fully functional in multiplayer.