Command Blocks In Minercaft Giving items On Kills

Could I make it so that when I kill a player for the first time, a command block gives me an iron sword, on my second kill it gives me a diamond sword, on my third kill it gives me an enchanted item, and so on?

I also want a way to reset this, as well as a start button. I know a way that I can do this by doing /scoreboard objectives add objectivename playerKillCount, and then a comparator does /give @p iron_sword. However, if I am too far from the command block and another player is next to it, the command block will give the sword to them instead. Also, @p won't work for the whole world, because I have to be near it. Can somebody please help me with a way that I can do this?


First, as you said in your question, declare a scoreboard objective of type playerKillCount to track player kills:

/scoreboard objectives add PlayerKills playerKillCount

Next, create a dummy objective that will track which rewards a player has already been given:

/scoreboard objectives add KillRewardGiven dummy

Now, on a clock (any type will do so long as you can control execution order, I'll use a 20Hz /fill clock) you'll need two blocks for each reward. One to give the reward:

/give @a[score_PlayerKills_min=1,score_KillRewardGiven=0] iron_sword

Then another do mark that the player has already received the award:

/scoreboard players set @a[score_PlayerKills_min=1,score_PlayerKills=1] KillRewardGiven 1

Here's an image illustrating how you would do your second tier reward:

enter image description here


To reset this you can run these commands, either in chat or in a command block:

/scoreboard players reset * PlayerKills
/scoreboard players reset * KillRewardGiven

To turn it on and off, you could hook something like this up to the clock:

enter image description here