Datastore not saving

I am trying to save a table once the player leaves and I do not get any errors but it prints that it fails.

game.Players.PlayerAdded:Connect(function(player)
	local data
	
	local success, errormessage = pcall(function()
		data = Datastore:GetAsync(player.UserId.."PlayerInventory")
	end)
	
	if success then
		for i,v in pairs(Inventory.PlayerInventory) do	
			for i,v in pairs(Inventory.PlayerInventory) do
				if v.Name == game.ReplicatedStorage.ItemImages[v.Name].Name then
					local newimages = game.ReplicatedStorage.ItemImages[v.Name]:Clone()
					newimages.Parent = player.PlayerGui:WaitForChild("Inventory").Frame
				end
			end
		end
		
	end
end)

  game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		Datastore:SetAsync(player.UserId.."PlayerInventory", Inventory.PlayerInventory)
	end)
	if errormessage then 
	 print("failed")
             warn(errormessage)
	end
end)

here is the whole script

1 Like

One thing I noticed is that you’re doing player.Userid instead of player.UserId in the player removed script.
Also, you aren’t printing the error message.

1 Like

Fixed those, now it doesnt print anything, I’m guessing its a problem with the loading now?

How are you saving your data? How big is the player inventory? If your player’s inventory is over the datastore limit, it will not save.

  Inventory.ItemData = {
[1] = {Name = "Axe", Skin = "000",},
[2] = {Name = "Pickaxe", Skin = "000",},
[3] = {Name = "Hoe", Skin = "000",},

}

Inventory.PlayerInventory = {Inventory.ItemData[1], Inventory.ItemData[2], Inventory.ItemData[3]} 

I am saving the playerinventory using items from itemdata

I would like the full script to debug more.

I don’t think he’s looking for leaderboard stats, but he’s looking for an inventory to save.
Why would you bring this up?

That doesn’t make sense. He shouldn’t use a leaderboard saving system for this, a regular table would do. Any type of leaderstats value would only be cash, or maybe deaths and knockouts. What you want him to do is inefficient in my eyes.

You could try something like this,

 local ds = game:GetService("DataStoreService"):GetDataStore("ToolSave")

 game.Players.PlayerAdded:connect(function(plr)
 local key = "id-"..plr.UserId

 pcall(function()
 local tools = ds:GetAsync(key)
       if tools then
       for _, tool in ipairs(tools) do
       local tool = game.ServerStorage.Tools:FindFirstChild(tool)
             if tool then
             tool:Clone().Parent = plr:WaitForChild("Backpack")
             tool:Clone().Parent = plr:WaitForChild("StarterGear")
             end
          end
      end
  end)

end)


 game.Players.PlayerRemoving:connect(function(plr)
 local key = "id-"..plr.userId

   pcall(function()
   local toolsToSave = {}
   for i,v in ipairs(plr.Backpack:GetChildren()) do
   if v then
    table.insert(toolsToSave,v.Name)
    end
 end
     ds:SetAsync(key,toolsToSave)
    end)
end)

i said that was for leaderboards NOT tools, read the edit please, and i posted one for tools just now

…And why would you bring up one for leaderboards and not tools? He’s looking to store inventory data, not put a high score on someone’s leader stats.

sorry about that didn’t correctly read the post , but what about this one?

I think he is looking to store skins for the axe, pickaxe, and hoe items to save. It could possibly work if you switched a few things around, but still seems a tiny little bit inefficient.

1 Like

I solved it I looped through twice for some reason.

if success then
	for i,v in pairs(Inventory.PlayerInventory) do	
		for i,v in pairs(Inventory.PlayerInventory) do
			if v.Name == game.ReplicatedStorage.ItemImages[v.Name].Name then
				local newimages = game.ReplicatedStorage.ItemImages[v.Name]:Clone()
				newimages.Parent = player.PlayerGui:WaitForChild("Inventory").Frame
			end
		end
	end
	
end
2 Likes

I wrote you new functions that will (hopefully) run smoother. This is because your code seems a little messy and most likely might break in the future.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DatastoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local ItemImages = ReplicatedStorage:WaitForChild("ItemImages")

Players.PlayerAdded:Connect(function(Player)
	local PlayerGui = Player:WaitForChild("PlayerGui")
	local Inventory = PlayerGui:WaitForChild("Inventory")
	
	local Data = nil
	
	local Tries = 0
	repeat
		Tries = Tries + 1
		
		local Success = pcall(function()
			Data = Datastore:GetAsync(string.format("%dPlayerInventory", Player.UserId))
		end)
		
		wait(1)
	until Success or Tries >= 10
	
	if Data then
		for _,v in pairs(Inventory.PlayerInventory) do
			local NewImages = ReplicatedStorage:FindFirstChild(v.Name)
			
			if NewImages then
				NewImages:Clone().Parent = Inventory
			end
		end
	end
end)

Players.PlayerRemoving:Connect(function(Player)
	local Tries = 0
	repeat
		Tries = Tries + 1
		
		local Success, Error = pcall(function()
			Datastore:SetAsync(string.format("%dPlayerInventory", Player.UserId), Inventory.PlayerInventory)
		end)
		
		if Error then
			warn(string.format("Error: %s", Error))
		end
		
		wait(1)
	until Success or Tries >= 10
end)

Do make sure your code is clean and you don’t do things like v.Name == Thing[v.Name].Name.

2 Likes