Inventory Saving Issue

So have a Inventory folder in player’s data folder.

image

Let’s say for example that I have 6 cobalt bars in my inventory.

image

DataFolder gets saved in the datastore script in form of a table (That includes saving everything in Inventory folder, in this example cobalt bar with a value of 6 (6 cobalt bars)).

That’s where my problem comes in:

When it gets saved, and loaded later, value of cobalt bar is 0.
I tried printing the cobalt bar value right before it gets saved (in game.Players.PlayerRemoving function),
turns out that the value gets set to 0 when PlayerRemoving function gets called

It prints out 0 instead of 6.

image

But if I put that line of code inside of PlayerRemoving function in a Local script,

image

it prints out 6.

image

As you can see in local script it prints it out before the player disconnects, but in server script it gets print out after the player disconnects

That means in my datastore script when PlayerRemoving function gets called the cobalt bar value becomes 0 before it gets saved, so it saves the 0.

In LocalScript the code executes fast enough but datastore is done in a server script.

I’ve been trying to fix this for quite some time.

Any ideas on how I could fix this problem?

Tell me if I need to provide more info.

Thanks for taking your time to read this! :slightly_smiling_face:

You’re probably setting the Cobalt value from the client and since FilteringEnabled is forced on all games, it wont replicate to the server.

But if I put that line of code inside of PlayerRemoving function in a Local script

Seems like thats what is going on. Try printing the value when the player leaves from the server and if its still 0, its being set from the client.

1 Like

I am not sure myself but I found this YouTube video that may help.

If this helped you please like this comment/post and make this comment/post as being the answer.

2 Likes

Yess, it worked!
I can’t believe I didn’t realize that earlier. I’ve been dealing with this all day.
Thank you!

1 Like