DataStore not saving. (Or not loading. I don't know.)

I moved everything to the server, however it is still erring on save and load.

Current script:

local dss = game:GetService("DataStoreService")
local store = dss:GetDataStore("PlayerData")

game.Players.PlayerAdded:Connect(function(player)
	local success, err = pcall(function()
		local key = "userkey:" ..player.UserId
		local dataTable = store:GetAsync(key)
		local coins, level = table.unpack(dataTable)
		player.HiddenStats.Coins.Value = coins
		player.HiddenStats.Level.Value = level
	end)
	if success then
		warn("Loaded data!")
	elseif err then
		warn("Did not load data.")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, err = pcall(function()
		local key = "userkey:" ..player.UserId
		local data = {
			player.HiddenStats.Coins.Value,
			player.HiddenStats.Level.Value
		}
		store:SetAsync(key, data)
	end)
	if success then
		warn("Saved data!")
	elseif err then
		warn("Did not save data.")
	end
end)

game:BindToClose(function(player)
	local success, err = pcall(function()
		local key = "userkey:" ..player.UserId
		local data = {
			player.HiddenStats.Coins.Value,
			player.HiddenStats.Level.Value
		}
		store:SetAsync(key, data)
	end)
	if success then
		warn("Saved data!")
	elseif err then
		warn("Did not save data.")
	end
end)

What does the error say? Are the values just not saving or is there some other problem now? Are you still printing to see if they show up?

No, I stopped printing that. It just says “did not save data” when it fails because I do not know how to get the actual error code.

Have you published the game? Everything else looks good.

The game is published. It is the same game that I asked for feedback on earlier.

why are you doing?

if success then
		warn("Saved data!")

try

if success then
print("Saved Data!")

Or does it matter?

What’s the difference? I did it because I find warns to look better than prints.

If thats your preference just keep it like that i guess

And where did it error at the player added or removing? If you don’t know check if when you join it says “Did not save data.” or when you leave

It errs at both join and leave.

I think it may be erroring because you don’t have an actual value to save, did you make stats on a different script?

for i, v in pairs(data) do
print(v)
end

Try printing your data table to make sure it is correctly entered. Maybe i’m not catching something obvious, but I do not see a problem sorry.

When the print runs, the data prints fine, yet the saving still fails.

Ok, I was finally able to test your code, do you have Enable studio access to API Services enabled? Also, your parameter for the pcall is played. Delete that parameter and you should be good because this is overriding the player.UserId.

Instead of setting the value of the Coins from a LocalScript, just do it from the server side

What do you mean by this? Do you mean the player parameter that I passed into the pcall earlier?

Yes sorry i meant player. Take out that parameter and it should work. Also if you want to print the error message all you have to do is use the variable for the error message inside a print.

I was reading another topic where someone was having trouble, and I think the problem might be that in studio it will save on game:BindToClose() because the studio server closes when you stop. The problem might be that I am doing game:BindToClose(player) when there is no player parameter, but rather it’s a for loop (this might be confusing I am typing this reply fast)

The post that helped me discover this

Also, I already have the player parameter in the pcall removed.

I got the data working, it was that evil BindToClose lol

1 Like

A better bindtoclose would be:

local runService = game:GetService("RunService")

game:BindToClose(function()
    local players = game.Players:GetPlayers()
    local saved = 0
    for _, player in ipairs(players) do
        coroutine.wrap(function()
            local sucess = false
            local tries = 0
            repeat
                 local s, x = pcall(function()
                      local data = {
                          player.HiddenStats.Coins.Value,
                          player.HiddenStats.Level.Value
                      }
                      store:SetAsync("userkey:"..player.UserId, data)
                 end)
                 if s then sucess = true break else tries += 1 wait(6) end
            until sucess or tries >= 3
            saved += 1
        end)()
    end

     repeat 
          runService.Heartbeat:Wait()
     until saved >= #players
end)