3.1 KiB
title, category, mentions, nav_order, tags
| title | category | mentions | nav_order | tags | |||
|---|---|---|---|---|---|---|---|
| On Player Death | On Event Systems |
|
4 |
|
Introduction
Sourced By Bedrock Commands Community Discord
This system will run your desired commands on the event that a player dies.
Setup
To be typed in Chat:
/scoreboard objectives add alive dummy
If you prefer to have the objective added automatically on world initialisation, follow the process outlined in On First World Load.
System
BP/functions/on_player_death.mcfunction
scoreboard players set @a [scores={alive=!2}] alive 0
scoreboard players set @e [type=player] alive 1
#Your Commands Here (example)
execute as @a [scores={alive=0}] run say I died
scoreboard players set @a [scores={alive=0}] alive 2
Here we have used an /execute - say command as an example but you can use any command you prefer and as many as you require.
Just make sure to follow the given order and properly add the selector argument scores={alive=0} as shown for your desired commands.
Explanation
-
alive=0this means player is dead. -
alive=1this means player is alive. -
alive=2this means player is dead and we have run our desired commands on/from them. -
@aselector will target all players alive/dead so we use it to mark everyone as 0 'dead.'- Note: we will ignore 2 or it will end up making the commands execute on dead players again. We only want our commands to execute once.
-
@eselector on the other hand will only target players who are alive, so we can use this to mark all alive players 1 'alive.' -
Now that dead players are 0 and alive players are 1 we can use this knowledge to run our desired commands on the dead players.
- Keep in mind we need to set their score to 2 after or otherwise the commands will keep executing till they respawn.
Tick JSON
If you are using functions instead of command blocks, the on_player_death function must be added to the tick.json in order to loop and run it continuously. Multiple files can be added to the tick.json by placing a comma after each string. Refer to Functions documentation for further info.
BP/functions/tick.json
{
"values": [
"on_player_death"
]
}
If using functions, your pack folder structure will be be as follows:
<FolderView :paths="[ 'BP', 'BP/functions', 'BP/pack_icon.png', 'BP/manifest.json', 'BP/functions/on_player_death.mcfunction', 'BP/functions/tick.json' ]"
Note: the scoreboard names (in this case: 'alive') may end up being used by other people. Appending
_and a set of randomly generated characters after would be a choice that reduces the probability of collisions. Similar technique can be employed for the.mcfunctionfilenames. Ex:
alive_0fe678on_player_death_0fe678.mcfunction
