So, I’m working on a DataStore system that manages coins. I’ve tried many ways of getting the amount of coins from the player, but after I fixed a problem another one came up.
So, as of writing this, my plan is this:
When a player joins, they get the coins saved in the game’s GlobalDataStore. (Works 100% fine)
When a player leaves, the amount of coins should be saved. But since a ServerScript manages the DataStore stuff it needs to somehow retrieve the amount of coins from the player. The way I chose to go about it is to use a RemoteEvent and pass the amount of coins that way. But since the event triggers at the moment the player leaves the game, nothing comes back (I made it so the LocalScript handling the client side sends the amnt. of coins back) because the client is already gone.
Do any of you know how to work around this?
Part of the ServerScript:

Part of the Local (Client) Script

1 Like
I might know the issue. If you’re the only one on the server, the server will be shut down when you leave. This means that your scripts may not be running completely before the shutdown occurs.
To fix this, you’ll need to save player data when game.Players.PlayerRemoving fires, AND when the server shuts down. This can be accomplished using game:BindToClose(function()) and coroutines to save the data of every player on the server. Not only will this (maybe) be the solution to your problem, but it also adds protection against accidental server shutdowns!
Here’s an example:
Players.PlayerRemoving:Connect(function(plr)
saveData(plr)
end)
game:BindToClose(function()
for _, plr in pairs(Players:GetPlayers()) do
coroutine.wrap(saveData(plr))
end
end)
Hope this helps! If it still isn’t working, try printing various values throughout your script to try and track down any errors.
Also, just a note, I would highly recommend storing player data on the server, rather than on the client. You can create a dictionary to store all player data and add player’s data with a line s uch as serverData[tostring(plr.UserId)] = data. This makes it easy to fetch data while keeping it safe, organized, and synchronized on the server.
I’ll try this, thank you! Sorry for the late reply.
I used a workaround by autosaving every 20 seconds.