Prevent scoreboard score from dropping below zero

So I have this server I'm working on, but when a player makes a purchase, their money can drop to negative numbers. I have researched but could not find an answer. The Name is Money. Please tell me how!

1

1 Answer

You have to verify the player has enough money before they are allowed to purchase the item.

The command that you use to give the player an item and the command you use to remove the money need to have the proper target selector with arguments. The arguments will make sure they have enough money before it gives the item. In the case of the scoreboard, you will use the argument score_*name*_min=x where *name* represents the objective name you use to store money and x represents the minimum amount.

As an example, lets say 1 diamond cost 3 units of currency and Money is the objective name that stores player's currency. The commands to give the diamond and remove the money could be:

give @p[score_Money_min=3] minecraft:diamond 1 0
scoreboard players remove @p[score_Money_min=3] Money 3

Edit: Response to the comment.

A quote from the wiki:

Selecting targets by score [score_name=SCORE] — Selects only targets with a score in objective name of no more than SCORE. [score_name_min=SCOREMIN] — Selects only targets with a score in objective name of no less than SCOREMIN. For example, @a[score_points_min=30,score_points=39] will select all players with a score in objective "points" between 30 and 39 (inclusive).

6

You Might Also Like