Minecraft Blogs / Tutorial

Function Data Packs for Dummies #7 Part 1 | The Scoreboard: Where numbers are created, stored and juggled

  • 5,282 views, 2 today
  • 28
  • 9
  • 11
Bertiecrafter avatar Bertiecrafter
Site Moderator
Level 67 : High Grandmaster Stack
627
This series of tutorials will teach you how to create your very own data packs, assuming you know absolutely nothing about commands. Although data packs could just contain files that replace built-in assets like resource packs, these posts will focus on adding new features and mechanics to the game. In every part I assume you've read the previous parts.

Introduction

The main purpose of the scoreboard is to generate numbers, which can be used to target players in commands. Think about health, armor, hunger bars, amount of jumps, xp, amount of chests opened, amount of deaths and even how many times you tuned a noteblock. Those numbers are stored in objectives and are player specific.

This is Part 1 of 2, focused on the casual use of the scoreboard by server administrators for example.

Objectives

You can add an objective using:
/scoreboard objectives add <name> <criteria> [display]
/scoreboard objectives add deaths deathCount {"text":"Player Deaths","color":"red"}
Keep in mind that names must be unique and as a data pack creator, you should take extra care of this. A good way of making your objectives unique is by prefixing the name with an abbreviation of your username and datapack name. The uniqueness of datapacks in general is also discussed in this blog. For example, I could prefix all my objectives in a fireworks datapack with "bcfw_", where bc stands for Bertiecrafter. The display name only appears in listings and displays, but is not used in commands. It uses the same JSON format as /tellraw, which is explained in this blog.

All possible criteria can be found here. Note the "dummy" criteria, this is an objective that does not change scores by itself and is perfect to keep track of variables in your data pack. Also notice the tiny mention indicating that all statistics can be used as scoreboard objectives as well. Every time a statistic of a player would increase, so would the score of the player in that objective of the same type.

You can remove an objective using:
/scoreboard objectives remove <name>
/scoreboard objectives remove deaths
You can modify the objective display name using:
/scoreboard objectives modify <name> displayname <new display name>
/scoreboard objectives modify deaths displayname {"text":"Player Daths","color":"red","bold":true}
You can list all objectives using:
/scoreboard objectives listYou can put the objective on display using:
/scoreboard objectives setdisplay <displayType> [objective]
/scoreboard objectives setdisplay sidebar deaths
Every type can only display one objective, but one objective can be displayed at multiple locations. You can display the score below a player's nametag using "belowName". Use unicode emojis like ❤ in the display name to make the appearance fancier ("20 ❤" instead of "20 health"). You could also display the scores in the tab list using "list" or display them in a list on the side of the screen using "sidebar". Finally, use "sidebar.team.<color>" to only display the specified objective for people in a team of that color. The /team command won't be covered in this tutorial. In order to clear a display slot, simply don't specify an objective in the command above.

Players

Most commands are obvious:
/scoreboard players add <player> <objective> <amount> - Adds amount to the player's score
/scoreboard players remove <player> <objective> <amount> - Removes amount from the player's score
/scoreboard players set <player> <objective> <amount> - Sets the player's score to amount
/scoreboard players get <player> <objective> - Gets the player's score for the specified objective
/scoreboard players reset <player> [objective] - Clears all or objective specific scores for that player
/scoreboard players list [player] - Lists all tracked players or objectives + scores for a certain player

The player parameter can be any of the following:
  • A target selector, which is explained in the previous blog. This means that any entity can be on the scoreboard, not just players.
  • Any other string of text. This is great for custom displays on the sidebar. Make sure to add at least one character not allowed in usernames (this excludes a-z, 0-9 and _), because otherwise a player with that name could join and modify the score based on objective type and other mechanics in your data pack.
  • A hidden entry, created by prefixing any string by a hashtag. This is the recommended choice for settings, global variables and constants that are internally used by datapacks. They can never be updated without commands, because player names can't start with a hashtag. Note that they won't appear in any of the locations used by the setdisplay subcommand.
  • Simply an asterisk to indicate any of the tracked scores. In contrast to @e, this does include offline players and fake/hidden entries and does not include entities who don't have a score on the scoreboard.

The Health Criteria

The "health" criteria uses specialized rendering. If you put a health objective in the "list" display slot, the scores are displayed as actual health bars by default. You can enable or disable this for any objective using this command:
/scoreboard objectives modify <objective> rendertype (hearts|integer)

Challenge Yourself

After every tutorial, I'll include a section where you are challenged to apply what you've learned. I recommend you playing with what you've learned, it helps you getting familiar with new concepts and be able to find solutions to problems. You don't have to do exactly what's written below, you can always challenge yourself in a different way.

Imagine being an admin on a server hosting a PvP event. Add health bars to the tab list and a kill counter as sidebar. Make sure to use some fancy coloured text as display name. Also create a secondary objective filled with custom text to promote your (imaginary) server. Use the score values to order the pieces of text. Then simply create a redstone clock to switch every couple seconds between the kills and the server promotion sidebar.

What's next?

Next up we're going to look at the complex side of the scoreboard for use in data packs.
Subscribe if you want to get notified of new posts.

Function Data Packs for Dummies #7 Part 1 | The Scoreboard: Where numbers are created, stored and juggled
Function Data Packs for Dummies #7 Part 1 | The Scoreboard: Where numbers are created, stored and juggled
Tags

Create an account or sign in to comment.

2
12/19/2021 12:56 pm
Level 25 : Expert Engineer
Chamalowat
Chamalowat avatar
Thanks for this very nice post, I am going to have a lot of fun thanks to this!
1
08/26/2021 9:22 am
Level 1 : New Miner
Hazakii
Hazakii avatar
Hey Bertiecraft, really loving the blog so far and appriciating the work your putting in to making it.

Just a quick (probably long) question.

I wanted to implement random score values to my object and was wondering if there was anyway of achiveing this with java, as the wiki tells me this command only applies to BE edition sadly.

The idea being that when a player mines an ore for example (that being the statistic object) they generate a random score between 1-100 instead of just gaining one score, and if the number is within a certain range (i.e. 1-60) an execute if command could proceed and the block would be broken, loot would drop and eventually the ore would respawn. however, if the random score generated was 61-100 it would count as a fail and instead the score would reset and nothing would happen to the ore (essential the ore would respawn immediately and no loot would drop)

Im essentially trying to add rng % chances (60% success/ 40% fail rate ,etc), is this at all possible in java and if so could you enlighten me with your vast knowledge XD
2
08/27/2021 1:33 am
Level 67 : High Grandmaster Stack
Bertiecrafter
Bertiecrafter avatar
Recently we got a random value provider for predicates (which is the standalone version of loot table conditions you mentioned in a comment). I would create a predicate that is true 60% of the time and then use `/execute if predicate ...` (explained in next parts) to set a score. Based on the score, you can then do one or the other.

Predicate
{
"condition": "value_check",
"value": {
  "type": "uniform",
"min": 1,
"max": 100
},
"range": {
"min": 1,
"max": 60
}
}

#function file:
scoreboard players set #value myObj 0
execute if predicate ... run scoreboard players set #value myObj 1
execute if score #value myObj matches 0 run <any command that runs on fail (40%)>
execute if score #value myObj matches 1 run <any command that runs on success (60%)>
# Note that you cannot simply repeat "if predicate" twice, since it would generate 2 different random values.
1
08/26/2021 5:07 pm
Level 1 : New Miner
Hazakii
Hazakii avatar
After some more research I found something to do with loot tables that can be combined with scoreboards to give probabilities, using drop chances. Looks to be much easier in theory, would still love to hear what you think though.
1
08/09/2021 11:55 pm
Level 26 : Expert Dragonborn
EndermanDotDat
EndermanDotDat avatar
I don't understand this:
Quote

Players

Most commands are obvious:
/scoreboard players add <player> <objective> <amount> - Adds amount to the player's score
/scoreboard players remove <player> <objective> <amount> - Removes amount from the player's score
/scoreboard players set <player> <objective> <amount> - Sets the player's score to amount
/scoreboard players get <player> <objective> - Gets the player's score for the specified objective
/scoreboard players reset <player> [objective] - Clears all or objective specific scores for that player
/scoreboard players list [player] - Lists all tracked players or objectives + scores for a certain player
1
08/11/2021 12:34 pmhistory
Level 67 : High Grandmaster Stack
Bertiecrafter
Bertiecrafter avatar
Could you ask a more precise question? *If* you understand the concepts of players and objectives, the commands should be understandable. I'll give a short recap of the concepts, but feel free to respond with a question :P
TL;DR: Objectives and players
Objectives are like bags of numbers. Each objective contains one value per player. Objectives can be created with types that cause the values to increment automatically. The listed commands allow you to play with (add to/remove from/set/get/reset) one or more values, depending on which objective and player you specified.
1
05/02/2021 6:33 pmhistory
Level 27 : Expert Engineer
SUPERIONtheKnight
SUPERIONtheKnight avatar
Been reading through your blogs, and I like what I'm seeing so far! Even though I know most of this stuff, I managed to pick up on a few new things. :)

With that said, I do see something here that could be clarified further:
"Any other string of text. This is great for custom displays on the sidebar as long as a dummy objective is used. If any other type is used, a player with that name could log in and cause scores to change."

Fake player names in dummy objectives can still cause conflicts with other players if you aren't careful. Lets say that I have some game that uses a fake player named "Timer". "Timer" will start at 10000 ticks and count down until the game is over. I also want to keep track of every players score for doing something, and I'm using the same dummy objective to pull this off. Now if a player named Timer joins, he can extend the length of the game just by increasing his score! The game could also see the player "Timer"s score equal to the time left in the game, or something else entirely depending on how things are set up!

So how do you get around this problem? Well, instead you can specifically choose to pick a fake player with special characters that you can't use in a normal MC username. For example, I know that there are no players with the username "Timer:", because you can't use a colon when picking a username. Can refer to this link to see acceptable characters for a MC username: https://help.minecraft.net/hc/en-us/articles/360034636712-Minecraft-Usernames

That aside, really good job with these blogs! I might start recommending them to people alongside the Minecraft Wiki now. :)
2
05/03/2021 11:39 am
Level 67 : High Grandmaster Stack
Bertiecrafter
Bertiecrafter avatar
Thank you for the feedback! Another option is the hidden player of course, but I get what you mean. I'll see what I can do about the description.
1
07/09/2020 6:16 am
Level 15 : Journeyman Modder
MrlongchopsZ
MrlongchopsZ avatar
I'm making a data pack for 1.16 that detects when a player types in chat &<1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, b, k> then changes the colour of the text to the corresponding colour

But i don't know how to detect when people type in chat can you help?
1
07/09/2020 8:29 am
Level 67 : High Grandmaster Stack
Bertiecrafter
Bertiecrafter avatar
I don't think you can detect that. Also please don't comment multiple times xD
Planet Minecraft

Website

© 2010 - 2022
www.planetminecraft.com

Welcome