Minecraft – Need to make target selector only select one entity

minecraft-commandsminecraft-java-edition

I'm writing a function for a datapack in Minecraft. After /reloading, /function couldn't find the function, so I pasted each line into Minecraft to check for syntax and other errors. I found that the problem was in the commands that store the aoecloud's position into a scoreboard, and also in the ones that take those values and place them into the Motion nbt for an arrow. The issue is that according to Minecraft, only one entity is allowed in the target selector while my target selector is selecting multiple. I tried using a customName to fix this problem but it didn't work. How can I get the target selector to select the arrow/aoecloud created at the top of the function and not give me errors?

summon minecraft:arrow ^ ^ ^1 {pickup: 0, Tags:["sniperbullet"], CustomName:"\"sniperbullet\""}

summon minecraft:area_effect_cloud 0 0 0 {Tags:["sniperaoecloud"], CustomName:"\"sniperaoecloud\""}
execute as @e[type=minecraft:area_effect_cloud, tag=sniperaoecloud] at @e[type=minecraft:area_effect_cloud, tag=sniperaoecloud] rotated as @a[scores={gunshots=1..},nbt={SelectedItem:{tag:{display:{Name:"{\"text\":\"Sniper Rifle\"}"}}}}] run tp @s ^ ^ ^1

scoreboard objectives add sniperx dummy
scoreboard objectives add snipery dummy
scoreboard objectives add sniperz dummy

execute as @e[type=minecraft:area_effect_cloud, tag=sniperaoecloud, name=sniperaoecloud] store result score @s sniperx run data get entity @s Pos[0] 10000
execute as @e[type=minecraft:area_effect_cloud, tag=sniperaoecloud, name=sniperaoecloud] store result score @s snipery run data get entity @s Pos[1] 10000
execute as @e[type=minecraft:area_effect_cloud, tag=sniperaoecloud, name=sniperaoecloud] store result score @s sniperz run data get entity @s Pos[2] 10000

execute as @e[type=minecraft:arrow, tag=sniperbullet, name=sniperbullet] store result entity @s Motion[0] double 0.0001 run execute as @e[type=minecraft:area_effect_cloud, tag=sniperaoecloud, name=sniperaoecloud] run scoreboard players get @s sniperx
execute as @e[type=minecraft:arrow, tag=sniperbullet, name=sniperbullet] store result entity @s Motion[1] double 0.0001 run execute as @e[type=minecraft:area_effect_cloud, tag=sniperaoecloud, name=sniperaoecloud] run scoreboard players get @s snipery
execute as @e[type=minecraft:arrow, tag=sniperbullet, name=sniperbullet] store result entity @s Motion[2] double 0.0001 run execute as @e[type=minecraft:area_effect_cloud, tag=sniperaoecloud, name=sniperaoecloud] run scoreboard players get @s sniperz

scoreboard objectives remove sniperx
scoreboard objectives remove snipery
scoreboard objectives remove sniperz

scoreboard players set @e gunshots 0
scoreboard players set @s reloadSniper 50

FYI the goal of this function is to spawn an arrow in front of a player and get it to fly in the direction they are looking. Also, if you catch any other errors with my code please point them out to me.

Edit: Pasted my new code above. I redid the scoreboard stuff with EarthToAccess' suggestions, and now the function runs fine. I also added another execute to get the values from the scoreboard using @s as the area_effect_cloud but also be able to put them into the arrow's motion using @s. Unfortunately, the arrows go unpredictably flying everywhere from/around the coordinates 177 3 163 (I'm on a superflat test world) even when I'm nowhere near that point instead of spawning in one block in front of me. However, they are repeatedly unpredictable, so they might go flying off 10 blocks at 90 degrees and then stop dead and drop, but if I keep my cursor at the same angle they will do it again and again the same way. If someone could explain this behavior that would be very helpful.

Best Answer

the problem is that @e and any selectors you use with it can be ambiguous. if literally any other entities with your specified tags exist, it would work with them too, which is why it's throwing the error.

a workaround that i've used is execute as @e and continued from there, using @s as my selector at the end of my run segment. for example;

execute as @e[type=minecraft:area_effect_cloud,tag=sniperaoecloud,name=sniperaoecloud] store result score @s sniperx run data get entity @s