Datastore retrieving a nil value?

Hey, so I’m attempting to make an inventory system that retrieves items based on the user and goes through a dictionary of item list;

https://ghostbin.com/paste/q3pdn

Code being above,

When printing the value being encoded at the end it simply returns []
and post decoding it returns []

Any help would be appreciated.

What exactly are you encoding? The attached table would be nice.

Datastores are encoded and decoding automatically. I’m not sure what you are trying to do, decoding a nil value.

Decoding decodes a encoded value. Encoding should happen first. Also, this proccess it automatically done through datastores.

1 Like

In the future, please post your codeblocks on the thread itself as opposed to an external site.

Frankly, this code had me worried right from the get-go when I saw that you were saving data across two data stores rather than a single scoped DataStore or one sporting a table.

Part of the issue is your implementation as a whole. I’ve noticed several sources of error which will negatively impact you in the future:

  • Improperly partitioned data leads to bad code structure in the future.

  • Multiple DataStores typically encourage developers the wrong way and they end up having data-related issues.

  • Lack of protected calls as is strongly recommended (and good practice) for DataStore calls - this also helps for error handling and failure redundancy (don’t only do “GetAsync or defaultValue”).

  • Calling SetAsync every time data changes. Saving every time data changes is largely bad practice. This will result in DataStores throttling for one and for two, SetAsync itself is dangerous. Be sure to check out the Data Store Errors and Limits article.

  • Pointless JSON formatting (DataStore information is automatically encoded to and from a JSON or similar format on the backend - don’t reinvent the wheel).

Honestly I can see a lot of margin for error in your code. That being said, I suspect you’re saving malformed data to the DataStore. The issue you’re experiencing is very easy to debug yourself - have you tried that? You can simply print what you’re expecting be saved in a DataStore and see if what is sent to the output is valid or expected.

[] represents an empty value. This means that what you are trying to encode or decode doesn’t have any elements in it.

4 Likes

Got a good suggestion for a guide/open source I can learn from?

I don’t really have any resources to offer, though I have done many experiments with DataStores and worked with limitations and different practices in mind. The DevForum also has lots of threads related to DataStore issues, which you can look into when developing your knowledge around the application of DataStores.