[CLOSED] leaderstats variables not saving for DataStoreService? [closed]
Asked by
4 years ago Edited 4 years ago
I've been trying fix this problem of not saving my variables, I'm using 2 variables and I'm also using leaderstats with 2 intValues. I do not know why my data isn't saving. This is server script, and it's in ServerScriptService. The print statement at line 44 works, however, the print statement at line 64 doesn't.
01 | local DSS = game:GetService( "DataStoreService" ) |
02 | local mDS = DSS:GetDataStore( "MyDataStore" ) |
03 | game.Players.PlayerAdded:Connect( function (player) |
04 | local key = player.UserId |
06 | local leaderstats = Instance.new( "Folder" ) |
07 | leaderstats.Name = "leaderstats" |
08 | leaderstats.Parent = player |
10 | local wins = Instance.new( "IntValue" ) |
12 | wins.Parent = leaderstats |
14 | local xp = Instance.new( "IntValue" ) |
16 | xp.Parent = leaderstats |
20 | local success, ErrorCode = pcall ( function () |
21 | return mDS:GetAsync( tostring (key)) |
26 | wins.Value = data [ "Wins" ] |
30 | warn( "Error while saving: " ..ErrorCode) |
34 | game.Players.PlayerRemoving:Connect( function (player) |
35 | local key = player.UserId |
37 | Wins = player.leaderstats.Wins.Value, |
38 | XP = player.leaderstats.XP.Value |
40 | local success, ErrorCode = pcall ( function () |
41 | mDS:SetAsync( tostring (key),dataTable) |
46 | warn( "Error while saving: " ..ErrorCode) |
51 | game:BindToClose( function (player) |
53 | for _, v in pairs (game:GetService( 'Players' ):GetPlayers()) do |
56 | Wins = v.leaderstats.Wins.Value, |
57 | XP = v.leaderstats.XP.Value |
60 | local success, ErrorCode = pcall ( function () |
61 | mDS:SetAsync( tostring (key),dataTable) |
66 | print ( "Error While Saving:" ..ErrorCode) |
I've tested this both in-game and in studio and they do not work. Also, if possible, how can I change mDS:SetAsync
to mDS:UpdateAsync
? Any help is appreciated. Edit: I switched to DataStore2 (as recommended by some people and friends) and so far, it actually saved my data. To future people who read this, just because I used DataStore2, doesn't mean you should either. Use either DataStoreService or DataStore2, depending which one is easier. Best of luck, future developers!