How can I create a shop with currency in Minecraft, using the scoreboard command? [duplicate]

I previously knew how to make a shop using command blocks pre MC 1.13 before they changed a bunch of commands. I know the basics of /scoreboard and other commands but I'm really lost on this.

I would like to be able to add money to players, remove money, and allow buying and selling with command blocks. Preferably I'd like players to not go negative when buying.


Solution 1:

First, create an objective to track their money value with:

scoreboard objectives add money dummy Money

In the command above, dummy simply means that the objective is only updated by commands. Next, add a player to the newly created objective:

scoreboard players set @s money 100

Here, I'm adding myself to the money objective with an initial score of 100. So I initially have, let's say $100. Alternatively, you can use a single command to set the initial value for all players with:

scoreboard players set * money 100

Now, for purchases and sales, you can use the add and remove arguments:

scoreboard players add @s money 10
scoreboard players remove @s money 10

All of the above can be chained together using command blocks to create your store. Of course, you'll need to integrate commands like give into the mix, for example:

scoreboard players remove @p money 10
give @p minecraft:concrete