Datastore not creating variables?

I’m attempting to make a inventory system that saves items in a folder but it looks like its not creating
the variable when a player joins as I t’s not printing “Added Item”.


--[Varibles]--
local DatastoreService = game:GetService("DataStoreService")
local InventoryDatastore = DatastoreService:GetDataStore("InventoryDatastoreTe")

--[Player Added]--
game.Players.PlayerAdded:Connect(function(player)
	local Success, InventoryDataExists = pcall(function()
		return InventoryDatastore:GetAsync(player.UserId)
	end)
	
	if Success then
		print("Data exists for: ".. player.Name.. ", attempting to load.")
		local DataFolder = Instance.new("Folder")
		DataFolder.Name = "DataFolder"
		DataFolder.Parent = player
		wait()
		if InventoryDataExists then
			local Inventory = Instance.new("Folder")
			Inventory.Name = "Inventory"
			Inventory.Parent = player:WaitForChild("DataFolder")
			local InventoryDatastore = InventoryDatastore:GetAsync(player.UserId)
			wait()
			for _, Item in pairs(InventoryDatastore) do
				local ItemVarible = Instance.new("StringValue")
				ItemVarible.Name = Item.ItemName
				ItemVarible.Value = Item.ItemValue
				print("Added Item")
				wait()
			end
			print("Sucessfully loaded: ".. player.Name.."s".. " Data.")
		else
			print("Data doesn't exist for: ".. player.Name.. ", creating Data.")
			local Inventory = Instance.new("Folder")
			Inventory.Name = "Inventory"
			Inventory.Parent = player:WaitForChild("DataFolder")
			local M9 = Instance.new("StringValue")
			M9.Name = "M9"
			M9.Value = "Common, 100, 1000, True"
			M9.Parent = Inventory
		end
	else
		print("Unable to load: ".. player.Name.."s".. " Data.")
	end
end)

--[Player Leaving]--
game.Players.PlayerRemoving:Connect(function(player)
	if player:FindFirstChild("DataFolder") then
		if player:FindFirstChild("DataFolder"):FindFirstChild("Inventory") then
			local InventoryTable = {}
			local Items = player.DataFolder.Inventory:GetChildren()
			for i = 1, #Items do
				local Item = Items[i]
				local ItemTable = {
					ItemName = Item.Name,
					ItemValue = Item.Value
				}
				table.insert(InventoryTable, ItemTable)
			end
			InventoryDatastore:SetAsync(player.UserId, InventoryTable)
			print("Sucessfully saved: ".. player.Name.."s".. " Data.")
		else
			print("Unable to find: ".. player.Name.."s".. " Data. (Inventory)")
		end
	else
		print("Unable to find: ".. player.Name.."s".. " Data. (DataFolder)")
	end
end)

Any reason to its not loading or saving the data?
Also data does exist:
image

The issue is that you likely have an empty table saved. Possibly from a previous failed test of this system?

You’ll want to clear that data, maybe using Crazyman32’s data store editor plugin, and then try again and note any issues/errors that arise during the first visit of someone who hasn’t visited before.

If, for example, an error occurred while making the data for the first time, you might end up with an empty inventory being saved and then loaded every time after that. The only way to know is to start fresh and check the data is good the first time you join.

Also use Test Server in Studio which allows you to “leave the game” as a client but stay there as the server and check for any errors during the saving procedure.

I’ve changed the variable:

local InventoryDatastore = DatastoreService:GetDataStore("InventoryDatastoreTe")

Everytime I test.

Okay, so then follow my advice using Test Server to look for any issues during the first data creation and the first time it is saved. You can’t catch the save errors using a live server or Play Solo as easily as you can with Test Server.

Your loading sequence seems fine (apart from the misleading “data exists” print which will print whether the data exists or not), so the problem most likely lies with the data generation or the saving.

Mhm, thanks for your help. I will look into it.


It prints out the values saved, I don’t know the error?