TransWikia.com

Execute as selector if nearest entity has a tag

Arqade Asked by Robomike on July 26, 2020

I want to execute a command to an armor stand if the nearest pig has a specific tag.

The commands that I have tried so far…

execute as @e[type=armor_stand] at @s if entity @e[type=pig,limit=1,sort=nearest,tag=carrot] run <command>

does not work because it checks if "carrot"-pig exists.

Having a pig with no tag "carrot" between the armor stand and the "carrot"-pig will just result to test passed.

execute as @e[type=armor_stand] at @s as @e[type=pig,limit=1,sort=nearest] if entity @s[tag=carrot] run <command>

might have worked but it changed the selector.

One Answer

Use the as parameter to switch executor to the armour stand at the end.

execute
  as @e[type=armor_stand,limit=1]      # ARMOUR STAND NOW HAS CONTROL
  at @s                                # Move the location to me.
  as @e[type=pig,limit=1,sort=nearest] # NEAREST PIG NOW HAS CONTROL
  if entity @s[tag=carrot]             # If I have the tag carrot, continue to the next step, otherwise stop.
  as @e[type=armor_stand,limit=1]      # ARMOUR STAND NOW HAS CONTROL
say The nearest pig to me has the carrot tag!

Correct answer by ExpertCoder14 on July 26, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP