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

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?


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,