Player NBT Data Storage

  • Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the comminuty!

    Now, what are you waiting for? Join the community now!

ShaneBee

Supporter +
Addon Developer
Sep 7, 2017
2,247
241
73
Vancouver, Canada
Often times you may notice you have a ton of variables for players. After a lot of players join your server you will notice your variable file growing and growing. One main issue here is that a lot of the players who join your server, may possible join once and never return.
This ends up clogging up your variable file if you don't manage the variables properly.

This snippet allows you to save data to a player's custom NBT rather than using variables.
Data saved this way is persistent with the player's data file.
(This requires SkBee 1.7.1+ and Minecraft 1.14+)

Here are 2 examples of some simple functions to set/get/delete values in the player's custom NBT.
code_language.skript:
# Simple function to set a value in a player's custom NBT
function set(p: player, key: string, obj: object):
    set {_nbt} to nbt compound of {_p}
    set {_c} to tag "custom" of {_nbt}
    set tag {_key} of {_c} to {_obj}
    set tag "custom" of {_nbt} to {_c}

# Simple function to get a value in a player's custon NBT
function get(p: player, key: string) :: object:
    set {_nbt} to nbt compound of {_p}
    set {_c} to tag "custom" of {_nbt}
    return tag {_key} of {_c}

# Simple function to delete a value in a player's custom NBT
function delete(p: player, key: string):
    set {_nbt} to nbt compound of {_p}
    set {_c} to tag "custom" of {_nbt}
    delete tag {_key} of {_c}
    set tag "custom" of {_nbt} to {_c}

And here are a couple examples of using these functions for setting/getting values.
code_language.skript:
# Example using the set function to set a value
on first join:
    set(player, "money", 100.0)
    set(player, "points", 0)
    set(player, "current-name", name of player)

# Example using the get function to get a value
command /money:
    trigger:
        set {_b} to get(player, "money")
        send "&aMoney: &b$&7%{_b}%"

Happy NBT-ing
 
Last edited: