Attempt to index nil with 'Id'

I’m making a system to save the id’s of the items I want to save, because the previous one saved the names and if I changed the name of an item it would vanish from the player’s inventory

The code:

print(ItemData[v.Name].Id..","..v.Name)
table.insert(itemsToSave,ItemData[v.Name].Id)

The issue is: it prints the right id and the name of the item but it gives the error “attempt to index nil with ‘Id’” when inserting in the table

In the ItemData(module) each item is separated like:

["Cool Item"] = {
		 Thing = 1;
			RandomThing = 20;
			Id = 5;
	};

Any help?

Can we see more of the code? I tested this and it works for me.


local sss = game:GetService("ServerScriptService")

local ItemData = require(sss.Modules.ItemData)


---Some Code
game.Players.PlayerRemoving:Connect(function(plr)
	local key = "id-" .. plr.userId
	local itemsToSave = {}
local success, errormessage = pcall(function()
		
		
		for i, v in ipairs(plr.Inventory:GetChildren()) do
			if v then
				
				print(ItemData[v.Name].Id..","..v.Name)
				table.insert(itemsToSave, ItemData[v.Name].Id)
					
				
			end
		end
		
			DS:SetAsync(key, itemsToSave)
			
	end)
	if success then
		print("SuccessFully Saved")
		for i,v in ipairs(itemsToSave) do
			print(v)
		end
	elseif errormessage then
		print("Something Went Wrong")
		warn(errormessage)
	end


end)

image
Inside the ItemData:

local ItemData = {
--- Same Thing
["Miner's Helmet"] = {
		SomeValues = 10;-- Aren't Important
		SomeValues = 5;
			SomeValues = 20;
			SomeValues = 0;
			Id = 27;
	};
	["Miner's Shirt"] = {
		SomeValues = 10;
		SomeValues  = 0;
		SomeValues = 20;
		SomeValues = 0;
			Id = 28;-- This is what I'm trying to focus on
		};
		
		
	}



Both of them are in ServerScriptService

There is something in your player inventory that is named differently to what you have it in your ItemData dictionary.

1 Like