Why isn't @p (at p) targeting the correct player?

I have a line of command blocks targeting @p, but they target seemingly random players. the person closest to the button will get teleported and given like 2 items, while a person 5 blocks behind them will get all the rest of the items and their gamemode updated.

I've tried rearranging the command blocks with and without chain command blocks, what do I do?


Solution 1:

Your issue probably is that you are teleporting them first. The simplest solution is to call the tp command last. You might be doing:

/give @p apple #Targets nearest player
/tp @p 5 10 7 #Teleports them farther
/gamemode @p survival #Still gets nearest player, but someone else might be closer now

Simply swap the teleport command to be last:

/give @p apple #Targets nearest player
/gamemode @p survival #Changes gamemode of nearest player
/tp @p 5 10 7 #Teleports player