Attempt to index field '?' (a nil value)

Hi, I am trying to make a crafting system for a simulator I have been working on but sadly the error has stopped me from doing so.

I keep getting the same error, attempt to index field ‘?’ (a nil value), on lines:

if v["Name"] == playerData["Hats"][itemName]["Name"] and v["Golden"] == nil then

if deleteAmount > 0 and v["Name"] == playerData["Hats"][itemName]["Name"] and v["Golden"] == nil then

The code:

inventoryEvents["Golden"].OnServerInvoke = function(player, itemName)
	local playerData = _G.PlayerData[player.Name]
	
	local goldenRequirement = 10
	
	local normalCount = 0
	local itemActualName
	
	if itemName ~= nil then
		
		for i, v in pairs(playerData["Hats"])do
			if v ~= nil and v["Name"] ~= nil and playerData["Hats Equipped"][i] == nil then
				if v["Name"] == playerData["Hats"][itemName]["Name"] and v["Golden"] == nil then
					normalCount = normalCount + 1
					
					if normalCount >= goldenRequirement then break end
				end
			end
		end
		
		if normalCount >= goldenRequirement then
			
			local deleteAmount = goldenRequirement
			local itemActualName = playerData["Hats"][itemName]["Name"]
			
			for i, v in pairs(playerData["Hats"])do
				if v ~= nil and v["Name"] ~= nil and playerData["Hats Equipped"][i] == nil then
					if deleteAmount > 0 and v["Name"] == playerData["Hats"][itemName]["Name"] and v["Golden"] == nil then
						playerData["Hats"][i] = nil
						deleteAmount = deleteAmount - 1
						
						if deleteAmount <= 0 then break end
					end
				end
			end
			
			if deleteAmount <= 0 then
				storeGoldenPet(player, itemActualName, hatInfo["Hats"][itemActualName]["Rarity"])
				hatShopEvents["Refresh"]:FireClient(player)
			end
			
		end
		
	end
	
	return true
end

I have tried checking if the values are nil but that did nothing.

Please help, thanks.

2 Likes

The lines the error occurs on:

2 Likes

In order to find out which part of the statement is failing I suggest your print the values of the variables being checked before the if.

This is probably what’s causing that error.