Scripting Helpers is winding down operations and is now read-only. More info→
Log in to vote

Datastore stats not saving, despite changes. Am I missing an easy fix?

Asked by 1 year ago

I'm having trouble trying to figure out why the saving function in my datastore script isn't working. The script is a normal script object located in ServerScriptService. I can edit my stats manually when testing, but when save functions are carried out, the return values are the ones that are already saved and apparently just get saved again despite being changed. My code is as follows:

local DataStoreService = game:GetService("DataStoreService") local StatStore = DataStoreService:GetDataStore("StatData") local function saveData(player) local dataTable = { player.stats.Cash.Value; player.stats.Level.Value; player.stats.XP.Value } for i,v in pairs(dataTable) do print(i .. "index current value set to: " .. v) end local success, err = pcall(function() StatStore:SetAsync(player.UserId, dataTable) for i,v in pairs(dataTable) do print(i .. "index value now set to: " .. v) end end) if success then print("Data was saved!") else print("Data was not saved!") warn(err) end end --[[manual save function game.Workspace.SavePart.Touched:Connect(function(player1) local player = game:GetService("Players"):GetPlayerFromCharacter(player1.Parent) saveData(player) end) ]] game.Players.PlayerAdded:Connect(function (player) local leaderstats ="Folder") leaderstats.Name = "stats" leaderstats.Parent = player local cash ="IntValue") cash.Name = "Cash" cash.Parent = leaderstats local level ="IntValue") level.Name = "Level" level.Parent = leaderstats local xp ="IntValue") xp.Name = "XP" xp.Parent = leaderstats local data local success, err = pcall(function() data = StatStore:GetAsync(player.UserId) end) if success and data then print("Player has saved data! " .. data[1] .. "dollars, " .. data[2] .. " levels, and " .. data[3] .. " XP.") cash.Value = data[1] level.Value = data[2] xp.Value = data[3] else print("Player has no saved data.") end end) game.Players.PlayerRemoving:Connect(function(player) --save on leave local success, err = pcall(function() saveData(player) end) if success then print("Data has been saved upon player leaving.") --this prints but no actual data changes else print("Data was NOT saved upon player leaving.") end end) game:BindToClose(function() --crash save for _, player in pairs(game.Players:GetPlayers()) do local success, err = pcall(function() saveData(player) end) if success then print("Data has been saved upon shutdown!") else print("Data has not been saved upon shutdown!") end end end)

Answer this question