My inventory DATASTORE cannot save?

Sorry i tried my best to mess with datastore
please help.

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

local dataStore = DataStoreService:GetDataStore("INVEN")
local INV = {}

Players.PlayerAdded:Connect(function(player)
	local inventory = Instance.new("Folder", player)
	inventory.Name = "Inventory"
	
	local data = nil
	local success, err = pcall(function()
		local data = dataStore:GetAsync(player.UserId)
	end)
	
	if success and data then
		for i, item in pairs(data) do
			item.Parent = inventory
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	for i, name in pairs(player.Inventory:GetChildren()) do
		table.insert(INV,name.Name)
	end
	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, INV)
	end)
end)

game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do 
		local success, err  = pcall(function()
			dataStore:SetAsync(player.UserId, INV)
		end)
	end
end)

Are you testing this in studio?

1 Like

yes but i think that is not the problem because my coins can save

1 Like

Ok, so it just wont save the inventory table

1 Like

i think so, can you help with that?

1 Like

Untitled
e

I don’t think there’s nothing wrong with that, he just named it name for some reason

BUT; I did have issues with this before, @superender11111 try renaming it

i don’t even know how i accidentally mentioned someone else

2 Likes

ye mb i thought the dot was on the middle of da 2 parameter

What you are doing is setting a table inside of the script, that means every player will use the same table. Add a folder inside of the player, and everytime he buys something, add that to the folder. Add a

local INV = {}

in plrs.PlayerAdder, plr Removing and BindToClose, then when the player leaves, loop through the plr folder and add everything to the INV table, then save that table through datastore, then, when the player joins, loop through the table and search for the items inside of the table in your item folder (in serverstorage or wherever it is) and clone them to the player’s backpack

Dont forget to also add a

if not table.find(INV, item) then

So you don’t get duplicates

1 Like

i think the player remove last very quick so you need to move the loop that inserting table outside the function

I reworked the code and got it to work. For some reason this code

local success, err = pcall(function()
	local data = dataStore:GetAsync(player.UserId)
end)

isn’t working

I remade the code and commented out that part and it works. That part is pretty necessary so I’m stuck at the moment. But I will post the code here, But before you use it try to get the success, err pcall working again as it’s pretty important.

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

local dataStore = DataStoreService:GetDataStore("INVEN")

Players.PlayerAdded:Connect(function(player)
	local inventory = Instance.new("Folder", player)
	inventory.Name = "Inventory"

	local data = nil
	--local succes, errormsg = pcall(function()
		local data = dataStore:GetAsync(player.UserId)
	--end)
	--if succes then
		if data ~= nil then
			if data.Inventory then
				for i, item in pairs(data.Inventory) do
					local tag = Instance.new("StringValue")
					tag.Name = item
					tag.Parent = inventory
				end
			end
		end
	--end
end)

local SaveData = function(player)
	
	local dataToSave = {}
	dataToSave.Inventory = {}
	for i, name in pairs(player.Inventory:GetChildren()) do
		table.insert(dataToSave.Inventory,name.Name)
		print(name.Name)
	end
	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, dataToSave)
	end)
	if success then
		print("Saved")
	elseif err then
		warn(err)
	end
	
end

game.Players.PlayerRemoving:Connect(function(player)
	SaveData(player)
end)

game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do 
		SaveData(player)
	end
end)