Minecraft – Command Block Question for “if, then” statement

minecraft-bedrock-editionminecraft-commands

On the latest version of Minecraft Bedrock edition is it possible to have a command block (When activated) check for the nearest player's name and kill them if it does not add up to a pre-entered name?

For example, if "Player1" steps on the pressure plate that activates the command block they do not die, however, if "Player2" steps on the pressure plate that activates it, they die.

Edit: Using more than one command block.

Best Answer

Simpler solution

There is often a better command that can be used instead of /testfor, if you have a pressure plate that should only kill "Player2", you can simply use this command:

/kill @a[name=Player2,x=~,y=~2,z=~,r=1]

This is assuming that you have the command block 2 blocks under the pressure plate.

More general solution with /execute

Whenever you want to use /testfor, you can likely use /execute instead, simply by putting the target selector from the /testfor command into it.

If you want to kill "Player2" if a zombie is within 5 blocks distance from the command block, then you could use /testfor @e[type=zombie,r=5] in a command block and then /kill Player2 in a conditional chained command block.

This could be done more easily by putting the target selector from the first command into an /execute command:

/execute @e[type=zombie,r=5] ~ ~ ~ kill Player2

Even more general solution

In some extremely rare cases I can't think of any situation where you really need anything beyond /execute right now even this may not be enough, maybe you want to execute several commands as a specific group of mobs that may not be detectable with the same target selector after every command.

If you really need something like this, then you can set up a scoreboard objective:

/scoreboard objectives add objectives dummy

Setup for a even more general solution

You would use one repeating command block that continuously sets the score of all entities to 0, the scoreboard objectives will be used to determine what entities to execute as:

scoreboard players reset * objectives

For every system that you want to set up you would then set the scoreboard score with the same target selector you would use with /testfor, or /execute. Each score value should be unique to every setup:

scoreboard players set @e[type=zombie] objectives 1

You would then use a chain of command blocks, one for each command that you want to execute. You would likely use /execute again, maybe in a command like this:

execute @e[scores={objectives=1}] ~ ~ ~ kill @a[r=5]

This example would kill all players within 5 blocks of any zombie, it could be done more easily with /execute @e[type=zombie] ~ ~ ~ kill @a[r=5].