Item.Value is nil?

PROBLEM
I’ve created an datastore that saves and loads the players inventory and this works by using StringValues. But anyways to the point when the script gets to load the players inventory I keep getting an error for line 27 (26 Prints “Nil”) meaning that nothing exists when it does because when it saves the player it successfully does it.

OTHER
I change the datastore every time I test so I get no corrupted saves.
local InventoryDatastore = DatastoreService:GetDataStore("InventoryDatastoreTE")
I also made a psot like this around yesterday but that was an different issue with the datastore.

PHOTOS

CODE

print("Version: 0.4")

--[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 InventoryDatastoreData = InventoryDatastore:GetAsync(player.UserId)
			wait()
			for _, Item in pairs(InventoryDatastoreData) do
				print(Item.Value)
				local ItemSplit = string.split(Item.Value, ",")
				local ItemVarible = Instance.new("StringValue")
				ItemVarible.Name = ItemSplit[1]
				ItemVarible.Value = Item.Value
				ItemVarible.Parent = Inventory
				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 = "Gun"
			M9.Value = "Gun, M9, 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:WaitForChild("Inventory"):GetChildren()
			for i = 1, #Items do
				local Item = Items[i]
				print(Item.Value)
				table.insert(InventoryTable, Item.Value)
			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)

Try replacing Item.Value with just Item when referencing the data in the for loop.

1 Like

Add an if statement that checks if the Item exists before trying to do stuff with it, rather then just assuming it has a value.

I recommend doing this for most things to avoid unexpected nil values.