Attempt to index nil with number

I am working on a crafting system for my game it loads the crafting recipes form replicated storage and puts them in to an table (CraftingRecipes) but when i try to load part of a value that is in the table from that table it gives the error “attempt to index nil with number” on line 9.

local CraftingRecipes = {}
local NumberOfCraftingRecipes = 0
local CurrentCraftingRecipe = 0
-- >>:  Change Current Crafting Recipe
function ChangeCurrentCraftingRecipe(NewCraftingRecipeID)
	CurrentCraftingRecipe = NewCraftingRecipeID
	local CraftingRecipe = CraftingRecipes[NewCraftingRecipeID]
	local Items = game.ReplicatedStorage:WaitForChild("Items")
	local ItemID = CraftingRecipe[1] --- the error happens on this line
	local Item = Items:WaitForChild(ItemID)
	local ItemName = Item:WaitForChild("ItemName")
	script.Parent.ITEMNAME.Text = ItemName.Value
	local ItemsUsedString = ""
	local ItemsUsedString = ""
	for i,v in pairs(CraftingRecipe[3]) do
		ItemsUsedString = ItemsUsedString .. Items[v[1]].ItemName.Value .. "X" .. v[2] .. "\n"
	end
	script.Parent.ITEMSUSED.Text = ItemsUsedString
end

-- >>: Load Crafting Recipes
for i,v in pairs(game.ReplicatedStorage.CraftingRecipes:GetChildren()) do
	local CraftingRecipe = {nil,nil,nil}
	CraftingRecipe[1] = v.OutPut.Value
	CraftingRecipe[2] = v.OutPutAmmount.Value
	local ItemsUsed = {}
	local NumberOfItemsUsed = 0
	for o,b in pairs(v.Uses:GetChildren()) do
		local ItemUsed = {nil,nil}
		ItemUsed[1] = b.Item.Value
		ItemUsed[2] = b.Ammount.Value
		if ItemUsed[1] >= 0 and ItemUsed[2] > 0 then
			NumberOfItemsUsed = NumberOfItemsUsed + 1
			table.insert(ItemsUsed,NumberOfItemsUsed,ItemUsed)
		else
			warn("Ivalid-Item: Crafting-Recipe-Item-Used, Crafting-Recipe-ID: " .. v.Name .. "Items-Used-ID: " .. b.Name)
		end
	end
	CraftingRecipe[3] = ItemsUsed
	if CraftingRecipe[1] >= 0 and CraftingRecipe[2] > 0 and CraftingRecipe[3] ~= nil then
		NumberOfCraftingRecipes = NumberOfCraftingRecipes + 1
		table.insert(CraftingRecipes,NumberOfCraftingRecipes,CraftingRecipe)
	else
		warn("Ivalid-Crafting-Recipe: Crafting-Recipe, Crafting-Recipe-ID:" .. v.Name)
	end
end

-- >>: load screen

if NumberOfCraftingRecipes > 0 then
	ChangeCurrentCraftingRecipe(0)
	for i,v in pairs(CraftingRecipes) do
		if i == 1 then
			script.Parent.ScrollingFrame["0"].Text = game.ReplicatedStorage.Items[v[1]].ItemName.Value .. " X" .. CraftingRecipes[1][2]
			script.Parent.ScrollingFrame["0"].MouseButton1Click:Connect(function()
				ChangeCurrentCraftingRecipe(0)
			end)
		else
			local CraftingRecipeButton =  script.Parent.ScrollingFrame["0"]:Clone()
			CraftingRecipeButton.Parent = script.Parent.ScrollingFrame
			CraftingRecipeButton.Name = (i - 1)
			CraftingRecipeButton.Position = UDim2.new(0,0,0,(20 * (i-1)))
			CraftingRecipeButton.Text = game.ReplicatedStorage.Items[v[1]].ItemName.Value .. " X" .. v[2]
			CraftingRecipeButton.MouseButton1Click:Connect(function()
				ChangeCurrentCraftingRecipe(v[1])
			end)
		end
	end
else
	warn("No-Crafting-Recipes: Crafting-Recipes, ReplicatedStorage")
end
1 Like

what line is the error on? You’re probably indexing a thing you think is a table, but which might actually be nil

line 9 il edit the post to show that

The error message means you are trying to get a value from a non-existing table. In other words,

local CraftingRecipe = CraftingRecipes[NewCraftingRecipeID] -- this returns nil, not a table

I think the problem is when defining “NewCraftingRecipeID” before passing it to the function. This value is often 0, and any table will always return nil for 0 and negative indexes. Try calling the function ChangeCurrentCraftingRecipe with another value (1?).

5 Likes

Thank you,
that was the problem I change the line to

local CraftingRecipe = CraftingRecipes[NewCraftingRecipeID + 1]

And it works fine now.

3 Likes