Hello, after hours of struggling because I thought my system was poorly designed, I realized there was a very specific Minecraft bug. I provided a datapack ready to use to reproduce the bug if necessary.
The "lectern" command:
/setblock ~ ~ ~ lectern[has_book=true]{Book:{id:"minecraft:written_book",Count:1b,tag:{title:"",author:"",pages:['{"score":{"name":"@p","objective":"test"}}']}}} replace
Here, it displays the score of the nearest player, and it works fine. If you destroy and replace it in the same location, it will update the score.
But for some obscure reason, in the mcfunctions, if you destroy and then replace the block lectern , the lectern will retain the NBT of the first lectern.
As a result, I created a map that uses the mechanics of lecterns to store scores in books, but my map doesn't work because of this bug.
However, when you use the /data modify block command, it updates the chunk/data, and then it will work.
In Loop
tag @e[type=item] add check
setblock 0 0 0 lectern[has_book=true]{Book:{id:"minecraft:written_book",Count:1b,tag:{title:"",author:"",pages:['{"score":{"name":"@p","objective":"test"}}']}}} destroy
tp @e[type=item] @p
scoreboard players add @p test 1
In setup
scoreboard objectives add test dummy
forceload remove all
setworldspawn 0 0 0
tp @a 45111 100 44512121
gamerule doTileDrops false
scoreboard objectives setdisplay sidebar test
So with this code, you should have a book with your current score every tick, but you'll notice that you always have the same book...
For my personal use, I solved the issue by placing an empty lectern and then filling it with data modify...
Instead of placing it directly with the correct NBT because it doesn't work.
Related issues
Attachments
Comments

Duplicate of MC-117574 (see example 1 in reproduction steps).

I've seen the duplicate but in my case, I replaced the lectern with air (/setblock)
and 10 minutes later I replaced the lectern in the same place with a /setblock lecternwith NBT.
And even if the block was air for 10 minutes or more, it kept the old NBT.

Thank you for your report!
We're tracking this issue in MC-117574, so this ticket is being resolved and linked as a duplicate.
If you would like to add a vote and any extra information to the main ticket it would be appreciated.
If you haven't already, you might like to make use of the search feature to see if the issue has already been mentioned.
Quick Links:
📓 Bug Tracker Guidelines – 💬 Community Support – 📧 Mojang Support
📓 Project Summary – ✍️ Feedback and Suggestions – 📖 Game Wiki
-- I am a bot. This action was performed automatically! The ticket was resolved by one of our moderators, and I left this message to give more information to you.