Give a tag to @s if they do not already have the tag and remove the tag if they already have it

I am creating a function pack for the bedrock edition of Minecraft. In one of the commands, I want to give the tag ‘test’ to @s (the player running the command) if they do not already have the tag and remove ‘test’ if they already have it.

How would I do this?


Solution 1:

The following commands should do the trick:

execute @s[tag=test] ~ ~ ~ tag @s add has_test
execute @s[tag=!has_test] tag @s remove test
execute @s[tag=has_test] ~ ~ ~ tag @s add test
tag @s remove has_test

The logic is basically that if it has the tag, it will remember it for the whole command chain then.