Minecraft – How to teleport players after defeating a mob

minecraft-commandsminecraft-java-edition

In Minecraft, I am making an adventure map and I am wondering after a player kills a mob, such as a boss, how to teleport or use /tellraw after they kill it.

Best Answer

There are 3 main ways to do this:

1.) Simply test if the mob/boss in question is still present, if it's not do the stuff you want to do. The main disadvantage is that you can't directly target the player who killed the mob, but in some situations that's not really a problem. If it is a problem however:

2.) There are statistics for killing any mob that has a spawn egg which can be used to create a scoreboard objective. You can use this to test if a player has killed a certain kind of mob and target that player.

/scoreboard objectives add [arbitrary name] stat.killEntity.[Entity name]

For example:

/scoreboard objectives add zombieKills stat.killEntity.Zombie

This will create a scoreboard which increases the score of a player when they kill a zombie. @a[score_zombieKills_min=1] will then target those players. Just make sure to reset the score afterwards.

The main disadvantage of this method is that you can only use it for mobs which have a spawn egg and you can't specify anything else. If you want more control and (for example) only detect when a player kills a "custom mob" which has altered NBT data:

3.) Advancements can use the "player_killed_entity" trigger to run a function from the player who killed that entity. The advantage of this method is that it has way more options. More information of all the options can be found here: https://minecraft.gamepedia.com/Advancements#minecraft:player_killed_entity

For example:

{
    "criteria": {
        "example": {
            "trigger": "minecraft:player_killed_entity",
            "conditions": {
                "entity": {
                    "type": "creeper",
                    "nbt": "{powered:1b}",
                    "location": {
                        "biome": "desert"
                    },
                    "effects": {
                        "minecraft:slowness": {},
                        "minecraft:weakness": {
                            "amplifier": {
                                "min": 2
                            }
                        }
                    }
                },
                "killing_blow": {
                    "source_entity": {
                        "nbt": "{SelectedItem:{id:\"minecraft:wooden_sword\",}}"
                    }
                }
            }
        }
    },
    "rewards": {
        "function": "example:test"
    }
}

This will trigger and run the function "example:test" (located at "data\functions\example\test.mcfunction) when a player kills a powered creeper (in a desert with any kind of slowness effect and a weakness effect with a minimal amplifier of 2) with a wooden sword. This is of course really specific, but it shows how much control you have.

The function is run through the player who completed the advancement, which means you can use @s in the function to only target the player who triggered it.

Make sure to revoke the advancement in the function, since advancements can only be completed once without revoking them.