Hello,
I’ve got a strange problem with my DataStore.
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:
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 = 3function 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
endreturn data
endfunction 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 successreturn success, data
endfunction 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
endfunction 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
endgame.Players.PlayerAdded:Connect(function(Player) – Inventory
local tnew = Instance.newlocal ts = tnew(“Folder”)
ts.Name = “Inventory”
ts.Parent = Playerfor x = 1, #hasValues do
local tool = tnew(“BoolValue”)
tool.Name = hasValues[x]
tool.Value = false
tool.Parent = ts
endloadData(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. Many thanks!