Saving tools data with DataStore2

  1. Can you give me the name of the last tool?
  2. Where is it not disappearing from? The list? The datastore?

1- It doesn´t really matter the name. Suppose i have Tool1 and Tool2. If i delete Tool2, the Tool1 will remain, and vice-versa.
2- Its not disappearing from the datastore. If i leave the game and come back again, 1 tool will always remain. If i add more tools, leave and come back again, the number of tools will be correct, the problem only happends when removing the tools until the last one.

When the player joins, can you unpack the value from the datastore and print it? Also when setting the datastore can you unpack that too and print it?
if you don’t know what unpacking is, here’s an example
print(unpack(workspace:GetChildren()))
The above should print all of the children in the workspace.

hmm, i will try that, let me see…

Yes, it prints the name of a tool

In this specific case the name printed was sword

Can you play around with adding, removing tools, and rejoining and keep the prints stored somewhere like a txt file?

Yes, i can do that. One moment plz.

Hi there, could you properly format your code to use a codeblock so we can read it better (more formatting details here)? If you want to remove the tool and save it when the player leaves, you should be using the PlayerRemoving event. Could you better explain what you want to happen and what really happens?

Hi Qxest, i have edited the code and included the codelock as requested, thanks for the orientation! I believe may not be using the PlayerRemoving event because of how the DataStore2 module works. What i want to happend is that when a tool is removed, it will not remain in the player´s backpack when i leave and rejoin the game. For example: If i have 3 tools on the players backpack: Tool1, Tool2 and Tool3. If i remove Tool3, leave and rejoin the game, Tool1 and Tool2 will be in the game. That is the expected behaviour. However, if i keep removing until the last one, the very last tool will always remain when i rejoin the game. In summary, i can´t get rid of the last tool.

What do you mean by this exactly? How are you removing the last tool? You do know if a tool is equipped it’s not in Backpack but in the player’s character in Workspace.

I mean completely removing from the players backpack, and also not present in the character as an equipped tool. As if the tool is on the player backpack and then i use Destroy() or ClearAllChildren() on the backpack.

Sorry, I may be a bit slow here to understand. Just so I can see what’s going on, can you confirm that the adjust function is being called by sticking a print statement right above it. If it is being printed as you remove the 3rd tool, there is a problem with how you’re saving your tools (your code is unnecessarily complex btw). You keep saying you’re removing the last tool but I don’t see where in your code you’re doing that. All I see is that you’re saving it every time you call adjust and use the same datastore over and over. You probably don’t even need to do this, just update the datastore when the player leaves rather than every time his or her tools update.

I am removing by other means, that are not on this code (i mean by another script). But i made a print() on the adjust function as you suggested. I can get the print at everytime i add or remove a tool, except when it´s the last tool…

Regarding the fact that the script do not save only when the player leaves, is an attempt to prevent data loss, as explained here How to use DataStore2 - Data Store caching and data loss prevention
I understand that this may be a controversial method, but i have decided to give it a try.

What’s this line supposed to do? You won’t get any data if it’s nil.

The nil is because the module that i am using allow the use of a default value, and if you don´t want to use any default value you set it to nil.

Also, i am getting data, because the script is actually saving any alteration that i do. If the player gets a new tool on his backpack, leave the game, and then come back, the tool will be there. If the tool is removed, when he rejoin, it wont be there anymore. All of that works fine, except when we talk about the last tool.

I have found a solution for this problem. After making a few tests, i have figured that i had to check at the adjust function if both the backpack and the character had a tool, and if there is no tool, then save the data. I am messing around with DataStore2 for the purpose of testing, but i am still not sure if i will actually implement it in a game, because of some concerns Concerns about Berezaa's datastore saving method that have been raised about this method. Anyways, thanks for all the help!

2 Likes

Hello. Can you define an equipped tool drop event to update the data in the DataStore2?