Is there a way to determine whether a player is online or offline with command blocks?

Is there a way to determine whether a player is online or offline with command blocks? - Concentrated female entrepreneur typing on laptop in workplace

I have some redstone circuitry in place (see attached image), involving a hopper clock, command blocks, and the scoreboard. The basic purpose is to play a sound with /playsound, and then once a certain time has elapsed (490 ticks of the hopper clock, or ~245 seconds), play the music again if the player is in range.

The redstone circuitry

Scoreboard Objectives: timer (dummy)

Command Block #1 (closest to the hopper clock):

scoreboard players add @a[r=100] timer 1

Command Block #2 (adjacent to the first one):

scoreboard players set @a[score_timer_min=490] timer 0

Command Block #3 (use the process of elimination):

playsound records.mall @a[score_timer=0] ~ ~10 ~ 2.0

mall.ogg is not, in fact, my favorite choice. I selected it because the actual music being played is very nearly the exact length of what mall.ogg is replaced with in my resource pack.


The system works perfectly, with one exception. When a player relogs, or logs out and then logs back in at a later time, the music (obviously) does not resume playing, and does not start again until the timer reaches 490 and resets to 0. The way to fix it (from what I see) would be to detect when a player is not logged in, and set their timer score to -1. When they logged back in, they would hear the music begin one second after.

Is there a way to detect a player's absence and then modify their score, even though they are not online?



Best Answer

For this problem, you don't actually need to modify the players score when they are offline (which is possible, but finicky), you should be able to change their score as soon as they log back in.


First, create a scoreboard objective of type stat.leaveGame:

scoreboard objectives add quits stat.leaveGame

You should then have two command blocks on some kind of clock, one activated after another. In the block that is activated first, put this command:

scoreboard players set @a[score_quits_min=1] timer -1

Then, in the one that is activated second:

scoreboard players set @a[score_quits_min=1] quits 0

The quits score increments when the player leaves. So, when they log back on, it will be at 1 and their timer score will be set to -1. Their quits score then has to be set back to 0, so that it can be used for the next logout and also so that timer isn't repeatedly set to -1,




Pictures about "Is there a way to determine whether a player is online or offline with command blocks?"

Is there a way to determine whether a player is online or offline with command blocks? - From below of crop multiethnic team of professional basketball players gathering and putting hands together while standing on playground before game
Is there a way to determine whether a player is online or offline with command blocks? - Photo of Two People Using Their Gadgets
Is there a way to determine whether a player is online or offline with command blocks? - Woman Using Laptop



How can you tell if someone is online on Minecraft?

Simply insert their name instead of a selector in /execute if , like this: /execute if entity Horst run \u2026 This checks if that player exists in the world, which is only the case if they're currently online on that server/LAN game or playing that world in Singleplayer.

How can you tell a player using command blocks?

You should try to not use the scoreboard, and instead try joining the server and when a player quits the game, a yellow message will appear in the top left corner of the screen, saying [gamertag] has left the game.



How to detect a player using command blocks! | /execute command tutorial for 1.16 Minecraft Java




Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.

Images: Andrea Piacquadio, Andrea Piacquadio, cottonbro, Marcus Aurelius