(table expected got nil) invalid argument #1 to ‘pairs’ [error] , is it not finding the table?

I have these tables (equippeditems)

Näyttökuva 2022-08-12 172937

for _ in pairs(t) do return true end

is giving me an error (table expected got nil) invalid argument #1 to ‘pairs’
the table exists .
in this situation the table was Equipped[“Weapons”]

This script under , basically is meant to be a equip script , but under circumstances works for unequipping

local function isfilled(t)
	for _ in pairs(t) do return true end
	return false
end


if table.find(Items,Item) then --checks if player has the item unlocked
		

		if  isfilled(EquippedItems[Itemtype] == true then 
			
			EquippedGui:FindFirstChild(itemtype):FindFirstChild(Item).Parent = InventoryGui	
			table.clear(EquippedItems,itemtype)
		
		end	
		
		if itemicon:FindFirstChild("Equipped").Value == false then
			
				itemicon:FindFirstChild("Equipped").Value = true
				table.insert(EquippedItems[itemtype],Item)
			itemicon.Size = UDim2.new(1,0,1,0)
			itemicon.Parent = EquippedGui:FindFirstChild(itemtype)
		else
			itemicon.Parent = InventoryGui
			
			itemicon:FindFirstChild("Equipped").Value = false
			end

This line has two typos:

if  isfilled(EquippedItems[Itemtype] == true then
  1. Did you mean itemtype?
  2. isfilled() doesn’t end with a parenthesis.
for _ in pairs(t) do return true end

You should use pairs for dictionaries and ipairs for tables instead:

for _,_ in ipairs(t)

Also, your isFilled function can be simplified into

local function isfilled(t)
	return #t > 0
end
2 Likes

yeah , sorry I made a typo in the example code

Hello thanks for the help with the function, I might have found the “real” problem, not sure tho.

I get error ‘attempt to get lenght of a nil value’, at the function, so how does the table somehow become nil, is it the table.clear?

That means that EquippedItems[itemtype] must be nil, i would reccomend verifying the value of itemtype (Did you mean table.find(Items,itemtype) or isfilled(EquippedItems[Item])?)

actually the thing is that I am looking if there is anything the same type already equipped as the thing that is being equipped. (ik you deleted it but still)

is only used to identify if the player owns the item he wants to equip , since its an event on the server, the function is for checking if there is equipped a same itemtype

I was the one who recommended that function, AFAIK from his previously public code t was not an array-like table. # gets the number keys and evaluates to the greatest one, it doesn’t do anything with string keys or whatever type is used.