Minecraft Blogs / Tutorial

How to Create a Shop | Command Block Tutorial

  • 15,126 views, 6 today
  • 11
  • 4
  • 6
Code202 avatar Code202
Level 70 : Legendary Engineer
Hello! Today, you will learn how to create a shop with custom commands and variables that allow players to have money and buy items!

What We Are Doing:
Today, we will be producing code that will look something like this:
if (money >= itemCost) {
money =- itemCost
give @p item itemAmount itemValue
} else {
tellraw @p {"text":"You do not have enough money!","color":"red"}
If you didn't understand that, don't worry, it will all be explained through the tutorial.

Creating a Variable:
Variables in programming hold a value, and depending on this value, certain actions will be run.
Lets create a variable:
/scoreboard objectives add money dummy
If a variable, or score, is a dummy, then it will only increase or decrease whenever it is needed to.
Now, lets make the variable value appear above a players head:
/scoreboard objectives setdisplay belowName money
The variable money will now display above every player's head, which you may notice if you are in multiplayer.
Set your money value to two:
/scoreboard players set @p money 2
And if we want to add an amount, you can do the following:
/scoreboard players add @p money 2
Place a command block that is repeating and always active and enter:
/give @a[score_buy_min=1,score_buy=1,score_money_min=2] diamond 1 0
Add the following variable:
/scoreboard objectives add buy trigger
A trigger variable can act as a command, that can be run by players with and without OP.
Run this command in a repeating and always active command block:
/scoreboard players enable @a buy
In a chain command block above this, enter:
/scoreboard players remove @a[score_buy_min=1,score_buy=1,score_money=2] money 2
In a chain command block, that is always active, on top of the last command block, enter:
/scoreboard players set@a[score_buy_min=1,score_buy=1,score_money=2] buy 0
And now, you can repeat this chain, and if the money is at the minimum amount required to buy the item, then you will get the item and money will be deducted.
For example:
/give @a[score_buy_min=2,score_buy=2,score_money_min=5] emerald 1 0
/scoreboard players remove @a[score_buy_min=2,score_buy=2,score_money=5] money 5
/scoreboard players set @a[score_buy_min=2,score_buy=2,score_money=5] buy 0

By Code202

For 1.11.2+

Create an account or sign in to comment.

07/17/2019 3:05 pm
Level 1 : New Miner
kingofchaos972 avatar
tahnk you
06/26/2019 4:59 pm
Level 1 : New Miner
jazzbo5108 avatar
Doesn't work in pe
01/29/2018 10:50 am
Level 42 : Master Dragonborn
SpyroGamesNl avatar
does it work on 1.10/1.8/1.7.10?
07/29/2018 9:46 pm
Level 7 : Apprentice Engineer
KatzPlayMinecraf avatar
06/04/2017 8:25 am
Level 34 : Artisan Taco
FunkyMunkey avatar
06/05/2017 1:58 am
Level 70 : Legendary Engineer
Code202 avatar
Thanks :D
Planet Minecraft


© 2010 - 2023