My inventory doesn't save well, but everything else does?

I created a module that saves all my data when I leave the game, but it doesn’t appear to always save for my inventory as it’s having issues saving sometimes.

Here’s a function from the module that runs when the player leaves the game:

module.saveData = function(player)

		if player.Backpack then
			local key = player.userId.."-key"
			local stats = player:FindFirstChild("leaderstats")
			if stats then
				local lvl = player:FindFirstChild("LevelVal")
				local exp = player:FindFirstChild("CurrentExpVal")
				local haki = player:FindFirstChild("Haki")
				local beli = player:FindFirstChild("BeliVal")
				local bounty = stats:FindFirstChild("Bounty")
				local playerData = data:GetAsync(key)
				local Data = {}
				Data.lvl = lvl.Value;
				Data.exp = exp.Value;
				Data.haki = haki.Value;
				Data.bounty = bounty.Value;
				Data.beli = beli.Value
				Data.inventory = {}
				local savedWhichTool = {}
				for _,v in pairs(player.Backpack:GetChildren()) do
					if v:FindFirstChild("Saveable") and savedWhichTool[v.Name] == nil then
						print("saving " .. v.Name)
						savedWhichTool[v.Name] = true
						table.insert(Data.inventory, v.Name)
				if player:FindFirstChild("EquippedItem") then
					local eVal = player:FindFirstChild("EquippedItem")
					local name = eVal.Value
					if savedWhichTool[name] == true then
						table.insert(Data.inventory, name)
				savedWhichTool = {}

NOTE: Some stuff (like if statements) that look like they shouldn’t be there are only there because I was attempting to fix this issue for a while.


Why do you spawn a function right after the function is initiated? Also, why did you create 2 tables (Data.Inventory and savedWhichTool)? You could’ve just done if not Data.inventory[v.Name] then
to save some lives.

Anyway, could you please tell us what’s outputted in the console, what tools were originally in the StarterPack and which tools saved so I can help figure out what the issue is.

This thread lacks information on the most vital topic. Could you explain the circumstances around your save function? How is it not saving well? What issues are you receiving? Are you encountering any of the following symptoms?

  • Code errors (includes silenced errors)
    • Spawn destroys the stack trace, so any error you encounter in it may not show up.
  • Lack of budget to complete requests (throttling)
  • Outside interferences (editing via client, improper module call, unfinished save procedure)
  • Data loss (use a DataStore Editor to review the key and check if anything passed through)

Please be sure to debug your code or provide detailed information about your issue so a source of error can be determined.

In terms of your code, there’s a few improvements I suggest you make. This doesn’t quite resolve your issue but I feel that these updates would be useful for the time being while you’re also attempting to fix the problem with your module.

  • Don’t wrap your code in a spawn function, as there is no necessity to do so. Just run the function on each player and let it go from there, synchronously, across each player.

  • Consider not using the player as a data dependency (meaning a location where you store raw data), especially if you hide the leaderboard or don’t want player stats seen.

    • leaderstats is primarily meant to be a display configuration for the leaderboard which is a carry-over from many editions of the leaderboard.
  • I’m not sure if you’ve truncated this code sample, but if you haven’t, you are expending your budget unnecessarily with the GetAsync call (playerData variable). It does not affect write operations, but it does needlessly spend a request that could be used for other DataStore work.

  • GetChildren returns an array. You should use ipairs to iterate over arrays. It is designed for arrays and runs faster than pairs. It also guarantees ordered iteration at an i++ basis due to the indexes being known. pairs is more for cases where index is arbitrary or otherwise unknown.