0

I was setting up a welcome message to display to players that join my server when I came across something unexpected. I tried to run this command

execute @a ~ ~ ~ /list

I wanted every player to get a message in chat telling them who is online however when I run this command nothing appears in chat and the command block output just lists online players. Here is the output I got when I tested it just now with just me online:

[18:46:29] JakepLol

So my question is, why does this execute the command as if it is the command block running it and is there any other way I can achieve this?

2 Answers 2

2

Player List

The way that /execute works is not that the player runs the command, but that it sets the player as the effective "command sender", while keeping the command block or the opped user who ran the /execute command as the true command sender. This allows /execute to run op commands that the player could not run, but also means that the player does not see the output of these /execute commands, even if it was targeted to them.

However, you are not out of luck. Using the /tellraw command, you can display any message you want, including a selector.

/tellraw @a ["",{"text":"Welcome to the server! Here is a list of all online players: "},{"selector":"@a"}]

This command is special, because it allows you to format a message using JSON (JavaScript Object Notation) to allow colored text, scoreboard values, and selectors, and much more to be displayed in a chat message. There are a few similar commands, but /tellraw is the one you need.

You probably want to customize the text component to your liking, so here is a guide to creating the text components manually and here is a website that will allow you to create them automatically.

Player Count

To reproduce the /list command further using this method, you can use the /stats command, combined with a scoreboard. Create a new scoreboard of any name (PlayerCount), and set a fake player (#Count)'s score on that board to 0.

/scoreboard players set #Count PlayerCount 0

Now, it is time to use /stats. Within your command chain, before your /tellraw command, place a new command block with the following command:

/testfor @a

Then, stand on top of the command and run:

/stats block ~ ~-1 ~ set AffectedEntities #Count PlayerCount

Now, you will notice that each time your command chain runs, the player count score will be updated. You can edit your /tellraw text component to display this count. Here is an example:

/tellraw @a ["",{"text":"Welcome to the server! There are "},{"score":{"name":"#Count","objective":"PlayerCount"}},{"text":" players online now: "},{"selector":"@a"}]
4
  • 1
    Thanks for explaining how execute works. I'm guessing you can't get the max number of players for a server using /tellraw because i would like to be able to display and output like "4/20 players online"
    – Jake
    May 3, 2017 at 18:52
  • @Jake You are correct that it is not possible to get the total number of possible players unless you input it into the game yourself, but it is possible to get the number of currently online players! By using the /stats command, combined with the /testfor command, you can retrieve the amount of entities whenever you want. I can't fit an example into this comment, but here is a guide for the /stats command, and refer to the guide or generator I posted in my answer to get information about how to place the score into the tellraw command. May 3, 2017 at 19:01
  • 1
    Thanks for the guide on the stat command! I'm looking forward to making my own version of the /list command that I can run from command blocks!
    – Jake
    May 3, 2017 at 19:05
  • @Jake No problem! I updated my answer to have slightly better formatting and included a section about /stats, in case you or anyone else reading this is interested. May 3, 2017 at 19:14
3

When something runs a command, a "command sender" object is created that has some standard functions dealing with the command sender, such as obtaining position and sending a message to the chat.

The /execute command will essentially create a clone of that object, but replace bits and pieces (such as position and "sender" designation) to the target of the command. However, the function for "sending message to chat" will copy the original sender object's function.

So if a command block is the original sender, the command block receives the message. Likewise, if the player is the sender, then the player receives the message. Running the following in the chat yourself while there are multiple entities in the world will result in you seeing the list as many times as there are entities:

/execute @e ~ ~ ~ /list

If you want to display all online players in the chat, you can use /tellraw, which has the option to process a selector:

/tellraw @a ["Online players: ",{"selector":"@a"}]
1
  • 1
    Thanks for the detailed explanation, I'm guessing there is no way to replicate the output from running /list in chat because that outputs the number of players and the max number of players. In addition, if a player is in a team the @a selector in a tellraw command will include a prefix and suffix etc. Is there any way of getting something that looks exactly like the /list command or is the @a selector the best I can get?
    – Jake
    May 3, 2017 at 18:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .