Datastores wont save backpack properly + data loss?

I know that Datastores cant save instances, but they can save their properties.

basically I am trying to get the gears in the players backpack and another folder (their owned gears) saved by getting the names of the gears, and when they rejoin, the script will detect the names in the table and find the gears in serverstorage and clone to respected area (backpack OR owneditems folder)

here is the code to this function

function SaveData(Plr)
	
	local KillsData = Plr.leaderstats.Kills.Value
	local BricksData = Plr.leaderstats.Bricks.Value
	
	local OwnedGears = {};
	local GearsInBackpack = {}
	
	for i,v in pairs(Plr.OwnedGears:GetChildren()) do
		--if v and v.Name then
			OwnedGears[v.Name] = v.Name
		--end
	end
	for i,v in pairs(Plr.Backpack:GetChildren()) do
		if v.Name ~= "ZZChute" then
			GearsInBackpack[v.Name] = v.Name
		end
	end
	
	for i,v in pairs(OwnedGears) do
		print(v) -- It SHOULD print out all names in the folder (sometimes it would print out a fraction of the stuff)
	end
	for i,v in pairs(GearsInBackpack) do
		warn(v) -- It SHOULD print out all names in the backpack except one (sometimes it would print out a fraction of the stuff)
	end
	local Success, Err = pcall(function() -- I could put this in 1 table but i am too lazy
		LeaderboardData:SetAsync(Plr.UserId, BricksData)
		LeaderboardData2:SetAsync(Plr.UserId, OwnedGears)
		LeaderboardData3:SetAsync(Plr.UserId, GearsInBackpack)
		LeaderboardData4:SetAsync(Plr.UserId, KillsData)
	end)

	if Success then 
		print("Data has been saved!")
	else 
		print("Data hasn't been saved!")
		warn(Err)		
	end
end

Hi there! Are you getting errors?

no not at all,

like i said in the comment, it would sometimes not print out everything

Instead of using OwnedGears[v.Name] = v.Name, try table.insert(OwnedGears, v.Name).

yeah, i could try that. ill let you know after some testing! (since it happens uncommonly)

try using table.insert() for this

table.insert(OwnedGears, v.Name)

That’s exactly what I said. Scroll down. :sweat_smile:

oh lol, I didn’t realize sorry about that.

1 Like

Yeah as HyperD3v said, you must do table.insert. What you’re telling the Datastore is to find v.Name inside that table by doing OwnedGears[v.Name], to insert things inside a Table you must do table.insert and that is the only way. Hopefully this helps you understand a little more about how Table’s work, have a good day :slight_smile:

Could you post the getting data part?

I hope you did the getting data part, I mean if you didn’t do it then it can’t get the data, it saves, but it can’t get the data and also use table.insert(), how HyperD3v said.

yeah, i have been using Table[#Table + 1] to insert stuff that eventually gets deleted later, i should use table.insert() for more precise things.

It seems to work! maybe I probably haven’t done enough testing. if it happens again I will let you know!

1 Like

Alright! Feel free to message me on the DevForum if you still need help!