Invalid argument #3 (string expected, got nil)

I am trying to get a text value to change by calling a function to get a value. I call this function a lot before this moment but when I call it i get an error saying invalid argument #3 (string expected, got nil)

I call this function using this line:
currSlot.Amount.Text = findAmt(playerInventory[v], currItemName)

the function is:

local function findAmt(inventory, item)
	for i,v in pairs(inventory) do
		if v[item.Name] then
			return v[item.Name]
		end
	end
end

im not sure what is causing this to occur because even if i do tostring(currItemName) it still gives me an error.

try this

currSlot.Amount.Text = findAmt(playerInventory[v.Name], currItemName)```

I tried that but then I just got invalid argument #1 to 'pairs' (table expected, got nil)

then the inventory is nil


Probably the thing which is happening here ig

If i print out the inventory it prints out the contents i get values.
image

playerInventory[v]

What is V?

Should you be passing in playerInventory instead of a value in that table?

I have my inventory divided into sections so v represents a number 1-4. I also have an if statement that checks to see if the item is there.

try if v[item.Name] ~= nil then
print()

if it didn’t print then check the

inv loop bc v is probably nil in that loop

it printed the value so its not nil in the inv loop, any other suggestions?
image

If currItemName being passed in is a string then change item.Name to item. You may be referencing .Name on a string hence the nil error.

CurrItemName is being split from ["Coal"] = 1 so coal is the item name and the amount is 1.
edit: I changed item.Name to item and it didn’t work

before setting the Amount.Text print the function

local test = findAmt(playerInventory[v], currItemName)

print(test)

and show me the output

it prints nothing
image

yes bc as i told u v is currently set to nil

edit: there is something wrong in the loop i guess

any suggestions that i could use to fix this?

I usually start doing

print(typeof(variable)) to track down why my comparators are misbehaving.

try showing the script for the v representing the numbers

What is output of

for i,v in pairs(item) do print(i,v) end

Inventory isn’t nil , there is no value in the table inventory with the key v.Name