Using testfor to give a potion effect
How do you use a testfor command to give you a potion effect? I want a command that makes it so when you hold a stick called "Flashlight", you get night vision.
Can anyone help?
Best Answer
/testfor will not be useful as it simply outputs if there was a match and will not allow you to target the one(s) who matched.
You will want to use /scoreboard instead, which can apply a label based on a target's NBT data either in the form of a score or "tag". Tag labels are much more efficient to use for simple boolean situations like this one.
The SelectedItem compound holds a copy of the player's currently-held item on the hotbar, minus the Slot tag. You can apply a label based on that, though it must be removed first in the event the player is no longer holding the item:
All command blocks are set to "Always Active" and "Unconditional".
Remove the label from players first, so that if they are no longer holding the item they will not receive the effect anymore.
/scoreboard players tag @a[tag=flashlight] remove flashlightAdd a "flashlight" label to players that are holding a stick named "Flashlight".
/scoreboard players tag @a add flashlight {SelectedItem:{id:"minecraft:stick",tag:{display:{Name:"Flashlight"}}}}And you can then use the
/effectcommand to correctly target the players who matched, as they will have a "flashlight" label now:/effect @a[tag=flashlight] minecraft:night_vision
Pictures about "Using testfor to give a potion effect"



How do you give a potion effect with commands?
Using the effect command in Minecraft Depending on what the player wants to do, there are different syntaxes for the command. In order to give an effect, the syntax is: "/effect give <targets> <effect> [seconds] [amplifier] [hideParticles]". In order to remove an effect, it is: "/effect clear <targets> [effect]".How to give a mob potion effects (forever) through commands.
Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.
Images: Ioannis Ritos, Gift Habeshaw, SCREEN POST, Darya Sannikova

