Table Problem(s)

Hello! It’s me again. I have been experimenting with tables and things like that again. Then I’ve found a function called table.find(). I’ve been trying that function, however, when I tried using it, some problems appeared.

To explain my problem I am gonna use an example:


local ThingsToPick = {
  ['1'] = {
 'Food'
}
}

local Things = {
['Food'] = {
 ['Fruits'] = {
     'Apple','Banana'
}
}
} --[[ This is where things are going to be listed in ]]


local function GetData(Input)
 if table.find(Things,Input) ~= nil then
 warn(Things[Input]['Fruits'][1]) 
-- The problem is that if I try using the word "Food" passed in the argument "Input", the table.find function will return as nil
   end
end

GetData(ThingsToPick['1'][1])

How can I fix that?

Any help is appreciated :slight_smile:

table.find works only with arrays. You are trying to use it with a dictionary.

Well, that’s the first thing which came into my mind, however using my previous example, I also tried

if Things[Input] then
-- Do something
end

but that also didn’t help, is it the same problem?

GetData(ThingsToPick[1]) will send ‘Food’ as argument

You are using a dictionary, that is your first issue. It’s not even good practice to use strings instead of numbers anyways, because with numbers you can mark the table as an array. Use dictionaries if your index is not a number.

Secondly, table.find works only for one-dimensional arrays, in a sense, values directly under indexes.

When I tried your code with Things[Input] it did warn ‘Apple’.

1 Like