DataStore Doesn't Save for All Users

Hello,

I’ve got a strange problem with my DataStore. :slight_smile:

I have two DataStore serverscripts, one for the Leaderstats, one for the Inventory. There are zero problems with the Leaderstats script. As for the Inventory one, it works just fine in Studio, and when testing it with my account on the site, it works fine, data is retained. However, when I tested the game with another account, the alt account’s Inventory did not save, but the Leaderstats did.

Here’s an image of the error I receive, where you can also see the confirmation of the Leaderstats saving correctly:

InvError

I’ve marked the errors below for Line 53 and Line 108 for where the errors appear, which are:

for x = 1, #data.tbl_1 do – Where the error pops up for Line 53

and

loadData(Player) – Where the other error appears for Line 108

The full script is below:

local module = {}
local DataStoreService = game:GetService(“DataStoreService”)
local InventoryItems = DataStoreService:GetDataStore(“XXXXXXX”)
local ToolGiverCheck = require(script.ToolGiverCheck)

datastore_success = {}

hasValues = require(script.HasValues) – included a snippet of the script below.

AUTOSAVE_INTERVAL = 120
DATASTORE_RETRIES = 3

function createData()
local data = {}
data.tbl_1 = {}
data.tbl_2 = {}

for x = 1, #hasValues do
table.insert(data.tbl_1, hasValues[x])
data[hasValues[x]] = false
end

return data
end

function dataStoreAttempt(dataStoreFunction)
local tries = 0
local success = false
local data = nil
repeat
tries = tries + 1
success = pcall(function() data = dataStoreFunction() end)
if not success then wait(1) end
until tries == DATASTORE_RETRIES or success

return success, data
end

function loadData(Player)
local Key = “PlayerIdNum-”…Player.UserId…"_ToolItems"
local InvData = Player:WaitForChild(“Inventory”)

local success, data = dataStoreAttempt(function()
return InventoryItems:GetAsync(Key)
end)

if success then
if not data then
data = createData()

  	print("Successfully created new inventory data for player '" .. Player.Name .. "'  (" .. Player.UserId .. ")")
  else
  	for x = 1, #data.tbl_1 do -- Where the error pops up for Line 53
  		local value = data.tbl_1[x]
  		local inv_value = InvData:WaitForChild(value)
  		
  		inv_value.Value = data.tbl_2[value]
  	end
  	
  	print("Successfully loaded inventory data for player '" .. Player.Name .. "'  (" .. Player.UserId .. ")")
  end
  
  datastore_success["id_"..Player.UserId] = true

else
print(“Failed to load inventory data for player '” … Player.Name … “’ (” … Player.UserId … “)”)
end
end

function saveData(Player)
if datastore_success[“id_”…Player.UserId] then
local Key = “PlayerIdNum-”…Player.UserId…"_ToolItems"
local InvData = Player:WaitForChild(“Inventory”)
local data = createData()

  for x = 1, #data.tbl_1 do
  	local value = data.tbl_1[x]
  	local inv_value = InvData:WaitForChild(value)
  	
  	data.tbl_2[value] = inv_value.Value
  end
  
  local success, data = dataStoreAttempt(function()
  	InventoryItems:SetAsync(Key, data)
  end)
  
  if not success then
  	print("Failed to save inventory data for player '" .. Player.Name .. "'  (" .. Player.UserId .. ")")
  else
  	print("Successfully saved inventory data for player '" .. Player.Name .. "'  (" .. Player.UserId .. ")")
  end

end
end

game.Players.PlayerAdded:Connect(function(Player) – Inventory
local tnew = Instance.new

local ts = tnew(“Folder”)
ts.Name = “Inventory”
ts.Parent = Player

for x = 1, #hasValues do
local tool = tnew(“BoolValue”)
tool.Name = hasValues[x]
tool.Value = false
tool.Parent = ts
end

loadData(Player) – Where the other error appears for Line 108

repeat wait() until Player.Character
end)

game.Players.PlayerRemoving:Connect(function(Player)
saveData(Player)

datastore_success[“id_”…Player.UserId] = nil
end)

spawn(function()
while wait(AUTOSAVE_INTERVAL) do
for i, Player in pairs (game:GetService(“Players”):GetChildren()) do
print("Auto-saving " … Player.Name … “'s inventory data”)
saveData(Player)
end
end
end)

return module

Here’s a snippet of the “HasValues” sub-script it draws from, which has 900 total items inside:

return {
“Song144”,
“Song145”,
“VIPAccessPass”,
“FirstTimePlayer”
}

I confess, I’m not too sure why one account’s data is being saved, but the other is not. I’d appreciate any advice on how to correct whatever may be the issue. :slight_smile: Many thanks!

In this case, it seems like tbl_1 in data is nil. The simplest check is to see if tbl_1 exists, and create it if it doesn’t.

Thank you for your response! I agree, but there’s no logical reason I can see why one account is able to retain their data, but the other does not.

I’ll purchase things on the second account, it survives respawns, but when the account leaves the game the data does not save, the error messages in the image appears, and I don’t know why. =/ But the other account that does save? No problem on leaving the game, I get the proper saved messages.

I’ve gone through and added a lot of Prints to track the process. Same result, it fails to load the data at Line 53, for the second player:

for x = 1, #data.tbl_1

Anyone have any thoughts as to why the data loads for the first player but not the second? :confused:

It appears that the root problem here was that my Inventory DataStore and my Leaderstats DataStore had the same key name. Whoops! :smiley: