Datastore Money Value is not saving correctly, any help?
I have this modified script mostly inspired by Youtube and it doesn't fully work. The Kills value saves perfectly, unlike the Money. What happens is that after a few hours to days, most of the total Money earned will reset or decrease about 80-90%. Being a nearly essential part of the game, I believe resetting the value will just erase everybody's total progress and possibly encounter the problem again. This is a trend in all players that exceed 1000 in cash. I currently have more than 2000 kills, and I should have three times the amount in cash, however my data just erased several times and I currently have about 450 Money.
As I got this tutorial on how to make it from YT, I am not sure how to troubleshoot or fix this.
Note: Money is gained from killing players and NPCs, and is used on things to buy. Kills are just collected.
01 | local datastore = game:GetService( "DataStoreService" ) |
02 | local ds 1 = datastore:GetDataStore( "MoneySaveSystem" ) |
03 | local ds 2 = datastore:GetDataStore( "KillsSaveSystem" ) |
05 | game.Players.PlayerAdded:Connect( function (plr) |
06 | local folder = Instance.new( "Folder" , plr) |
07 | folder.Name = "leaderstats" |
08 | local money = Instance.new( "IntValue" , folder) |
10 | local kills = Instance.new( "IntValue" , folder) |
13 | money.Value = ds 1 :GetAsync(plr.UserId) or 0 |
14 | ds 1 :SetAsync(plr.UserId, money.Value) |
16 | kills.Value = ds 2 :GetAsync(plr.UserId) or 0 |
17 | ds 2 :SetAsync(plr.UserId, kills.Value) |
19 | money.Changed:connect( function () |
20 | ds 1 :SetAsync(plr.UserId, money.Value) |
23 | kills.Changed:connect( function () |
24 | ds 2 :SetAsync(plr.UserId, kills.Value) |
28 | game.Players.PlayerRemoving:Connect( function (player) |
29 | local success, errormessage = pcall ( function () |
30 | ds 1 :SetAsync(player.UserId.. "-money" ,player.leaderstats.Money.Value) |
34 | print ( "Player Data successfully saved." ) |
36 | print ( "Error in saving Player Data." ) |