Help with saving an player inventory data

I’m trying to save this data but with the return on the pcall function returns nill and i need to load all items on the player’s iventory.

DataScript2

here is where i add an item to the player’s inventory

DataScriptOutput

1 Like

Hey, could you please send the script by the devforum? That way it’s easier to test it out and help you. You can do it like this:

print('This is lua code")

You can do this by highlighting your text and clicking on this icon over here:

image

The script that receives amd updates the data when any item is added on it

local datastore = game:GetService("DataStoreService")

local IventoryData = datastore:GetDataStore("PlayerInventory")

local Items


game.Players.PlayerAdded:Connect(function(player)
	
	local PlayerId = player.UserId
	
	
	local Folder = Instance.new("Folder", player)
	Folder.Name = "Iventory"
	
	local PlayerHasItemsOnInventory, PlayerItemsOnInventory = pcall(function()
		
	return	IventoryData:GetAsync(PlayerId)
		
	end)
	
	if PlayerHasItemsOnInventory  then
		
		for i, v in ipairs(PlayerItemsOnInventory) do
			
			local Item = Instance.new("StringValue", Folder)
			Item.Name = v
			
		end
		
	end
	
	
	local function UpdatePlayerDataWithCurrentInventory()
	
	Folder.ChildAdded:connect(function()
		
		Items = Folder:GetChildren() -- Handle All items on the players folder in one variable every time the event fires
			
			

			local function SavePlayerIventoryData()

				IventoryData:SetAsync(PlayerId, Items)


			end

			spawn(SavePlayerIventoryData)	
							
		end)
	
	
	end
	
	
	spawn(UpdatePlayerDataWithCurrentInventory)
end)

Before doing the ipairs loop, do this:

if not PlayersItemsOnInventory then
    PlayersItemsOnInventory = {}
end

Also the PlayerHasItemsOnInventory will doesn’t men’s they have it, it just means that the datastore call didn’t error.

DataScript4

keep the same error

I didn’t write PlayerHasItemsOnInventory, I said PlayerItemsOnInventory.