On Player Leave
Introduction
Sourced By Bedrock Commands Community Discord
This system will run your desired commands on the event that a player leaves the world.
Note: You cannot execute commands on the players that leave using target selectors. However, you may use the On Player Join system to execute when they join back.
Setup
To be typed in Chat:
/scoreboard objectives add total dummy
If you are working with functions and prefer to have the objective added automatically on world initialisation, follow the process outlined in On First World Load.
System
scoreboard players reset newPlayerCount total
execute as @a run scoreboard players add newPlayerCount total 1
scoreboard players operation newPlayerCount total -= playerCount total
#Your Commands Here (example)
execute if score newPlayerCount total matches ..-1 run say a player has left the world
scoreboard players reset playerCount total
execute as @a run scoreboard players add playerCount total 1
Here, we have used a /say
command as an example, but you can use any command you prefer and as many as you need.
Just make sure to follow the given order and properly apply the /execute if score
condition as shown for your desired commands.
Explanation
newPlayerCount
this fake-player-name means the total number of players on the world in the current game-tick.playerCount
this fake-player-name means the total number of players that were on the world in the previous game-tick, but also saves the total count to be used in the next game-tick.
The count is obtained using the Entity Counter system. It may be beneficial to refer to that page to better understand this one.
By subtracting 'playerCount' total from 'newPlayerCount' total, we will be able to identify if the player count has:
- decreased
..-1
- increased
1..
- or if it's unchanged
0
If it has decreased, we know that 1 or more players have left the game. Using this knowledge, we can run our desired commands from 'newPlayerCount' if it's score is -1
or less.
ie, if there were 10 players and someone leaves:
- that is
newPlayerCount - playerCount
- which results
9 - 10 = -1
- hence, we will detect by
..-1
- that is
The 'newPlayerCount' total is obtained first, subtraction is performed after that to run your desired commands, and lastly, the 'playerCount' total is obtained to be used in the next game-tick.
TIP
All commands involved in a command-block-chain or function will only run in a sequence one after the other but it all still happens in the same tick regardless of the number of commands involved. We are able to achieve this system due to the fact that commands run along the end of a game tick after all events such as player log in, log out, death, etc.. occur.
Tick JSON
If you are using functions instead of command blocks, the on_player_leave
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.
{
"values": [
"on_player_leave"
]
}
If using functions, your pack folder structure will be as follows:
- 📝on_player_leave.mcfunction
- 📝tick.json
- 🖼️pack_icon.png
- 📝manifest.json
NOTE:
The scoreboard names (in this case: 'total') 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 .mcfunction
filenames. Ex:
total_0fe678
on_player_leave_0fe678.mcfunction