When i have a onPlayerRemoving function that fires a remote event. The remote event doesn’t have time to fire before the player has left.
Here is the onPlayerRemoving function:
Generally what I’d do in this situation is Cache the data server sided, and check to see if the player still exists if the player doesn’t exist save the cache and then remove it so it doesn’t get altered any further.
It looks like your client’s data is on the client. This is unfathomably easy to exploit. Consider pushing ALL datastore related code to the server.
Also, keep in mind that your second function gives the client direct access to their own data. You should consider only keeping their data on the server, while giving the client its own “readable” version so it can present the data to the player.
Whether the question was about exploiting or not, I will still help someone get their code to work properly and practical. The code present is 100% not practical and I will offer advice on that.
I’ve edited my reply to be more direct with the topic, but please don’t try to regularize my reply when I patch my note about something so severe about his methodology.
An experienced player that plays your game a lot decides to spend 2000 robux on your game’s dev product. They decide to leave the game a little later… The remote event request is sent to the server… The datastore has an error and cannot save the player data at that exact time. The data never gets saved because there is no datastore error handling… The player rejoins and sees that their newly bought items were never saved, even though they paid for it… Then that player decides to never play again.
When the player leaves, you should add their player ID to a table…
Seeing the code that OP is working with, changing the location of the save data isn’t going to do anything. They need to convert their save method to be completely server-sided and get rid of the unnecessary RemoteEvent communication.
If data was held under the Player object, it would be a replicated instance and have two copies; one on the server and one on the client. It’d still be “on the server” and not require any conversion.
It’s not proper to keep data under the player because the player is effectively removed from the server after they leave unless a memory leak is keeping their Player instance from being garbage collected and dissociated completely.
I think you mean that it should be moved to ServerStorage, which is a more viable solution. Anything under the player should only be used for session data or display.