Command Block should only activate if someone logs on [duplicate]

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

/scoreboard objectives add quits stat.leaveGame

Next, put this commands into a always active repeating command block:

/execute as @a[scores={quits=1..}] at @s run <COMMAND TO RUN>

And in a chain command block in the direction that the first command block points to:

/scoreboard players set @a[scores={quits=1..}] quits 0

The command you put in the place of <COMMAND TO RUN> will now run every time a player rejoins the game.


It's very simple. Using functions it'd look like:

Repeating command block:

execute as @a[tag=!Joined] run function1

function1:

<insert commands>
tag @s add Joined

Using Command Blocks

  1. Repeating, always active:

    execute as @a[tag=!Joined] run <inert command you want to run>
    
  2. Chain, conditional:

    <commands>
    
  3. Chain, conditional

    execute as @a[tag=!Joined] run tag @s add Joined