Is there a way to give a player a score if they are holding a certain item [duplicate]

This is a good place to use scoreboard tags as opposed to a scoreboard objective. This will allow you to tag any player holding a snowball and use that tag to target them in a command without having to setup an objective to track a score.


Use 2 command blocks in a chain. The first command block removes the tag from all players. The second command block tags any player holding a snowball. These command blocks have to remain loaded. I recommend putting them in the spawn chunk. I recommend silencing command block output to prevent chat spam for op's. Use command:

/gamerule commandBlockOutput false

The first command block is RepeatUnconditionalAlways Active with command:

scoreboard players tag @a remove Holding

The second command block is ChainUnconditionalAlways Active with command:

scoreboard players tag @a add Holding {SelectedItem:{id:"minecraft:snowball"}}

Now you can use this tag within the target selector to exclude the players who are currently holding a snowball. In your case possibly:

/effect @e[r=1,tag=!Holding]

If you still want to give a player a score instead of a tag, it would be the same solution as above except the commands would be:

scoreboard players set @a Holding 0

And:

scoreboard players set @a Holding 1 {SelectedItem:{id:"minecraft:snowball"}}

Also to note, if the snowball is custom named or special in some other way, you can probably use parts of the data tag portion of your current testfor command in the data tag of the 2nd command block command.