Multiple target selectors in the same command

I am trying to testfor if a player fulfills two different criteria. One criteria cannot be changed and is: score_money=5000. I have tried to come up with another criteria to target a very specific player. For example I've tried:

/testfor @p[tag=buyHSR,score_money=5000]

/testfor @p[l=30,score_money=5000]

/testfor @p[score_buy=1,score_money=4500]

The problem, all of these commands work on singleplayer, and not on multiplayer. What happens on multiplayer is that it will testfor the score_money=500 of the nearest player and execute the following commands despite whether or not that same player has the score_buy=1 tag. So, if I am closest to the command blocks and have insufficient money but my friend has enough, who is farther away, the selector will find my friend but execute the commands for me. (This causes my money score to reach into the negatives). In summary, the target selector favors one criteria over the other depending on who has one of the criteria. I am trying to get a selector that will succeed only if both citeria are met.
I am running Minecraft 1.12.2.


Solution 1:

Your target selectors are not favoring one criteria over another.

When you add arguments to the @p target selector variable, it selects the nearest player who meets all the criteria even if there are other players closer who do not. In other words, it does not select only the closest player and test them to see if they meet the criteria.

I recommend using a radius or volume dimensions with or without coordinates in the target selector.

The easiest solution would be to use a radius:

/testfor @p[score_money=5000,r=10]

This will select the nearest player who has a money score of 5000 or less, and is within 10 blocks of the command execution.

Note: This is not testing for a player who has at least 5000, but 5000 or less. This may be why you are going into negative numbers. Anyone with less then 5000 within 10 blocks will satisfy this.

/testfor @p[score_money_min=5000,r=10]

The above will testfor the nearest player who has 5000 or more and is within 10 blocks.

Like I mentioned in the comments, the testfor command is not a good way to do most things. In your case it finds anyone who does meet all the criteria and if it finds any player who does, a conditional block after it will execute. Instead use the above target selector in whatever would be the next command.

Lets say you are selling stone for 5000 money, the commands could be:

give @p[score_money_min=5000,r=10] stone 1
scoreboard players remove @p[score_money_min=5000,r=10] money 5000

Another way you could do it, you could scoreboard tag the nearest player and then test the player with that tag:

scoreboard players tag @p add Nearest
give @a[score_money_min=5000,tag=Nearest] stone 1
scoreboard players remove @a[score_money_min=5000,tag=Nearest] money 5000
scoreboard players tag @a remove Nearest