1. 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!

Dismiss Notice
This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Player NBT Data Storage

Discussion in 'Snippets' started by ShaneBee, Nov 7, 2020.

  1. ShaneBee

    Moderator Resource Staff Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,179
    Likes Received:
    218
    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 (Skript):
    1. # Simple function to set a value in a player's custom NBT
    2. function set(p: player, key: string, obj: object):
    3.     set {_nbt} to nbt compound of {_p}
    4.     set {_c} to tag "custom" of {_nbt}
    5.     set tag {_key} of {_c} to {_obj}
    6.     set tag "custom" of {_nbt} to {_c}
    7.  
    8. # Simple function to get a value in a player's custon NBT
    9. function get(p: player, key: string) :: object:
    10.     set {_nbt} to nbt compound of {_p}
    11.     set {_c} to tag "custom" of {_nbt}
    12.     return tag {_key} of {_c}
    13.  
    14. # Simple function to delete a value in a player's custom NBT
    15. function delete(p: player, key: string):
    16.     set {_nbt} to nbt compound of {_p}
    17.     set {_c} to tag "custom" of {_nbt}
    18.     delete tag {_key} of {_c}
    19.     set tag "custom" of {_nbt} to {_c}
    20.  
    And here are a couple examples of using these functions for setting/getting values.
    Code (Skript):
    1. # Example using the set function to set a value
    2. on first join:
    3.     set(player, "money", 100.0)
    4.     set(player, "points", 0)
    5.     set(player, "current-name", name of player)
    6.  
    7. # Example using the get function to get a value
    8. command /money:
    9.     trigger:
    10.         set {_b} to get(player, "money")
    11.         send "&aMoney: &b$&7%{_b}%"
    12.  
    Happy NBT-ing
     
    #1 ShaneBee, Nov 7, 2020
    Last edited: Nov 23, 2020
    • Friendly Friendly x 2
    • Useful Useful x 2
  2. Eyst

    Eyst New Member

    Joined:
    Aug 13, 2020
    Messages:
    9
    Likes Received:
    0
    awesome. time to never use this because it is too much like datastore2 in roblox and I have ptsd of them.
     
  3. ShaneBee

    Moderator Resource Staff Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,179
    Likes Received:
    218
    I would appreciate replies that are on topic to this thread, please and thank you!
     

Share This Page

Loading...