Inventory Data Store

Hello! So I want to make an Inventory Data Store. It prints the data is saved but when i re-join the values aren’t there!

local DataStoreService = game:GetService("DataStoreService")
local InventoryDataStore = DataStoreService:GetDataStore("InventoryDataStore")

local RunService = game:GetService("RunService")

game.Players.PlayerAdded:Connect(function(player)
	local Inventory = Instance.new("Folder", player)
	Inventory.Name = "Inventory"
	
	local key = "pi_"..player.UserId
	
	local data
	local success, errorMessage = pcall(function()
		data = InventoryDataStore:GetAsync(key)
	end)
	
	if success then
		if data then
			for i, v in pairs(Inventory:GetChildren()) do
				v.Value = data.v
			end
			print("✅ | Loaded Inventory")
		else
			print("❎ | Could not load Inventory "..tostring(errorMessage))
		end
	end
end)

local function saveInv(player)
	local key = "pi_"..player.UserId
	
	local data = {}
	for i, v in pairs(player.Inventory:GetChildren()) do
		table.insert(data, i, v)
	end
	
	local success, errorMessage = pcall(function()
		InventoryDataStore:SetAsync(key, game:GetService("HttpService"):JSONEncode(data))
	end)
	
	if success then
		print("✅ | Saved Inventory")
	else
		print("❎ | Could not save Inventory "..tostring(errorMessage))
	end
end

game.Players.PlayerRemoving:Connect(saveInv)

game:BindToClose(function(player)
	if RunService:IsStudio() then
		return
	end
	
	saveInv(player)
end)

As mentioned in the quote above, I do not think you need a data.v there. Instead, use data.

1 Like

nope still doesn’t work​:thinking::thinking::thinking::thinking::thinking::thinking::thinking:

Do you receive any errors when running this code?

1 Like

nope i didn’t got any errors!!!

Are you trying this in game or in Studio?

Hmm… I would probably replace the dataStore code with this because I found many errors in the original one:

local DataStoreService = game:GetService("DataStoreService")
local InventoryDataStore = DataStoreService:GetDataStore("InventoryDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local Inventory = Instance.new("Folder", player)
	Inventory.Name = "Inventory"
	    	
	local data
    pcall(function()
         data = InventoryDataStore:GetAsync("pi_" .. player.UserId)
    end)

   if data then
        for i, v in pairs(Inventory:GetChildren()) do
				v.Value = data
		end
		print("✅ | Loaded Inventory")
   end

end)

game.Players.PlayerRemoving:Connect(function(player)
     local data = {}
     for i, v in pairs(player.Inventory:GetChildren()) do
		table.insert(data, v) -- Here, you put data, i, v instead of data, v. This meant that your get function couldn't read the data!
	 end

    pcall(function()
          InventoryDataStore:SetAsync("pi_" .. player.UserId, data)
    end)
end)

Nope the Inventory is still nil

Are you saving any values in the inventory. If so, are you doing it properly?

Yea I am storing string values

When joining the game, does the Inventory folder have these string values by default?

no it doesn’t have any string values

That’s why it isn’t working. You are asking the code to loop through the children of the Inventory folder when the Inventory folder does not have any children.

1 Like

oh… how can I fix it???

A possible fix for this is here:

local DataStoreService = game:GetService("DataStoreService")
local InventoryDataStore = DataStoreService:GetDataStore("InventoryDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local Inventory = Instance.new("Folder", player)
	Inventory.Name = "Inventory"
    local StringVal = Instance.new("StringValue", Inventory) -- Added string
    StringVal.Name = "String"
	    	
	local data
    pcall(function()
         data = InventoryDataStore:GetAsync("pi_" .. player.UserId)
    end)

   if data then
        for i, v in pairs(Inventory:GetChildren()) do
				v.Value = data
		end
		print("✅ | Loaded Inventory")
   end

end)

game.Players.PlayerRemoving:Connect(function(player)
     local data = {}
     for i, v in pairs(player.Inventory:GetChildren()) do
		table.insert(data, v) -- Here, you put data, i, v instead of data, v. This meant that your get function couldn't read the data!
	 end

    pcall(function()
          InventoryDataStore:SetAsync("pi_" .. player.UserId, data)
    end)
end)

Sorry to intervene. But I believe using the folder itself is apart of the problem because to use the folder you would need to store multiple instances ( instances being stringvalues, number values, etc.).

I recommend you just use the inventory data within table form. And to edit and view the table I also recommend making a module for managing the inventory data.

1 Like

What do you have in inventory stringvalues?
I will try something like this:

Ik it little bit foolish but is it local script?

The “Inventory” folder is empty, that is why it does not load the data, there is nothing inside the folder.

I tried to fix your code quickly, hope it works:

local DataStoreService = game:GetService("DataStoreService")
local InventoryDataStore = DataStoreService:GetDataStore("InventoryDataStore")

local RunService = game:GetService("RunService")

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

	local key = "pi_"..player.UserId

	local data
	local success, errorMessage = pcall(function()
		data = InventoryDataStore:GetAsync(key)
	end)

	if success then
		if data then
			for i,v in pairs(data) do
				local Add = Instance.new("StringValue")
				Add.Value = v
				Add.Parent = Inventory
			end

			print("✅ | Loaded Inventory")
		else
			print("❎ | Could not load Inventory "..tostring(errorMessage))
		end
	end
end)

local function saveInv(player)
	local key = "pi_"..player.UserId

	local data = {}
	for i, v in pairs(player.Inventory:GetChildren()) do
		table.insert(data, i, v)
	end

	local success, errorMessage = pcall(function()
		InventoryDataStore:SetAsync(key, game:GetService("HttpService"):JSONEncode(data))
	end)

	if success then
		print("✅ | Saved Inventory")
	else
		print("❎ | Could not save Inventory "..tostring(errorMessage))
	end
end

game.Players.PlayerRemoving:Connect(saveInv)

game:BindToClose(function(player)
	if RunService:IsStudio() then
		return
	end

	saveInv(player)
end)