Table function setting the whole table nil

Hello I come from my previous post (table expected got nil) invalid argument #1 to ‘pairs’ [error] , is it not finding the table? - #4 by grandisy

And before I made the post ,yesterday I thought that my problem might be that I am setting my table nil with table . clear.
So basically
How would it not set the table nil , but just clear the content of the table.
I tested this that when I used table.clear I printed my table and after clearing it in my script it came back as nil.

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

in this little function , it needs the table to not be nil for it to return the result ( basically checks if the table has something) , but it can’t if its nil

As i said in the previous post, you should verify if Itemtype really exists in EquippedItems. For example, with debugging.
It seems it doesnt because it returns nil.

1 Like

Check if the table is nil before running that function. There is no need to run that function on a table you just cleared because it checks if the size is greater than 0. Which you know it won’t be if you cleared it.

1 Like

Itemtype is actually "Weapons"and that is a very real thing inside of the Equippeditems

And, What does debugging tell you the value of itemtype in EquippedItems is?

its real the first time I use the function, so I have been trying to figure that out somehow

But, What is it’s value though?

itemtype well its searching it as the value of it , and the value is a string “Weapons” and that is inside of the Equippeditems

I have been repeatedly asking, what is the value of “Weapons” in EquippedItems?

Weapons is a table, that makes Equipped items a dictionary, Weapons will have a string value with the name of the item that has been equipped and for ex, it can be “Sword”

yes so what is the item that is equipped, what should be the value of EquippedItems["Weapons"]?!
and also

It can be “Sword”

shouldn’t it be {"Sword"}?

The item being equipped is a string “sword” that is added to EquippedItems[“Weapons”], or atleast should be, actually then it would make it an array, but yeah

Enabling type checking can help you find the problem.

at the beginning of your script, put:

--!nonstrict

or if you want to get flooded with warnings, with a higher chance of including the correct one,

--!strict

Okay, so Is it that the

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

function can’t do arrays

Aren’t you getting an error indicating t is nil?

It is an issue as far as I can see, the function I posted yesterday can do both arrays and dictionaries, but it doesn’t look like the main issue.

It would be good if you posted the current code with the table.clear, because of what @Cens_r said.

This function will error for anything passed that isn’t a string literal or a table literal.

local s = "Hello!" 
print(#s) --6 (length of string).
local a = {"a", "b", "c"}
print(#a) --3 (length of array).
local d = {a = 1, b = 2, c = 3}
print(#d) --0 (length of dictionary).

The unary length operator when used on a string returns the length (number of characters) of that string, when used on a table it returns the length (number of items) of the array part of the table.