My data store script is not saving data

Hi,
My data store script is simply not saving data, GetAsync returns nil. I don’t know why, it looks like everything is ok:

Players.PlayerAdded:Connect(function(player)
	local leaderstats = createLeaderstats()
	leaderstats.Parent = player
	
	local wins = createWinsContainer()
	local losses = createLossesContainer()
	wins.Parent = leaderstats
	losses.Parent = leaderstats
	
	local data
	local success, err = pcall(function()
		data = DataStore:GetAsync(player.UserId)
	end)
	
	if data == nil then
		data = {["Wins"] = 0, ["Losses"] = 0}
	end
	
	wins.Value = data["Wins"]
	losses.Value = data["Losses"]
end)

Players.PlayerRemoving:Connect(function(player)
	local success, err = pcall(function()
		DataStore:SetAsync(player.UserId, {["Wins"] = player.leaderstats.Wins.Value, ["Losses"] = player.leaderstats.Losses.Value})
	end)
end)

game:BindToClose(function()
	for _, player in ipairs(Players:GetPlayers()) do
		local success, err = pcall(function()
			DataStore:SetAsync(player.UserId, {["Wins"] = player.leaderstats.Wins.Value, ["Losses"] = player.leaderstats.Losses.Value})
		end)
	end
end)

Data stores do not take tables, sadly. Use an ordered Datastore or Datastore2

1 Like

But all my previous scripts where I use tables to save data work

I’m not exactly sure what’s happening here, but what @vycVascense said is false; DataStores can save table values.

Can you send us the full source of your script? Or at least include the part where you’re defining DataStore.

And, pcalls returning their status and any error message isn’t just for safety, it can help you.

Print out your pcalls “err” variables in PlayerAdded, PlayerRemoving, and to be safe, in BindToClose.
Try testing it then and let us know what it said, if anything.

2 Likes

Do you change your leaderstats value from local script? If you, the local changes not shown on server side.

1 Like

GlobalDataStore:GetAsync() would return a table with all values 0 but it returns nil

Are you sure that you saved data? Also,you can try use Profile Servcie

1 Like

I took the liberty of running a basic test, and according to my results, there is absolutely 0 reason this shouldn’t work.

Here’s a lightly modified version of your supplied code, as well as proof of it saving.

local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("TempDefault")

local PlayerData = {}

local function createLeaderstats()
	local a = Instance.new("Folder")
	local b = Instance.new("IntValue")
	local c = Instance.new("IntValue")
	a.Name = "leaderstats"
	b.Name = "Wins"
	c.Name = "Losses"
	b.Parent,c.Parent = a,a
	return a
end

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = createLeaderstats()
	leaderstats.Parent = player

	local data
	local success, err = pcall(function()
		data = DS:GetAsync(player.UserId)
	end)
	print(("[DEBUG] [PlayerAdded] [%s] Player Data from GetAsync:"):format(player.Name), data)
	if not success then
		warn(("[ERROR] [PlayerAdded] [%s] Data failed to load/get, error message: \n%s\n"):format(player.Name, err))
	end

	if data == nil then
		data = {["Wins"] = 0, ["Losses"] = 0}
	end

	leaderstats.Wins.Value = data["Wins"]
	leaderstats.Losses.Value = data["Losses"]
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, err = pcall(function()
		DS:SetAsync(player.UserId, {["Wins"] = player.leaderstats.Wins.Value, ["Losses"] = player.leaderstats.Losses.Value})
	end)
	if not success then
		warn(("[ERROR] [PlayerRemoving] [%s] Data failed to be saved, error message: \n%s\n"):format(player.Name, err))
	end
end)

game:BindToClose(function()
	for _, player in ipairs(game.Players:GetPlayers()) do
		local success, err = pcall(function()
			DS:SetAsync(player.UserId, {["Wins"] = player.leaderstats.Wins.Value, ["Losses"] = player.leaderstats.Losses.Value})
		end)
		if not success then
			warn(("[ERROR] [BindToClose] [%s] Data failed to be saved, error message: \n%s\n"):format(player.Name, err))
		end
	end
end)

Proof: (printed right after “data” is set to GetAsync)
image

Try this out (at least the printing) and let us know if anything comes up.

Edit: And if this is happening when testing in Studio, do be sure you actually have “Enable Studio Access to API Services” enabled in your Game Settings.

1 Like

Yes, I tested this script and it works well.

1 Like

That was it… thanks