How to check if something is in a dictionary

Im trying to check if something is in a dictionary.
It says here that Dictionarys cant use #TableName, so is there a way to check this?

I am a little unsure of what you’re asking here. If you are asking to check for something specific in a dictionary, that’s very simple:

local dictionary = {
     ["something"] = true
}

if dictionary["something"] then
     -- You found something in a dictionary
end

If you are otherwise trying to figure out how many items are in a dictionary, that’s fairly simple as well presuming you aren’t dealing with nested tables and recursion:

local dictionary = {
     ["something"] = true;
     ["something else"] = true;
     ["something else else"] = true;
}


function GetAmountOfItemsInDictionary()
     local amount = 0
     for _, v in ipairs(dictionary) do
          amount += 1
     end
     return amount
end

Hopefully this answers your question.

2 Likes

You’ll need pairs there instead of ipairs :slight_smile:

Also @Meat_Make are you just asking what a dictionary is and when you can and can’t use #?

Hello! What i started to do is put a dictionary instide a table, inside a table so:

 local Table = {
     {["Thing"] = 1381, ["Other Thing"] = "Yummy in my tummy"]},
     {["Oof"] = 83719, ["Other Oof"] = "Hello"]},
}

Idk if this will be helpful to you, but for instance when i use datastores from now on, and i need to index the data, i can use tables within tables within tables to accurately get each number as it comes when i return a for i,v in pairs(table).

1 Like
local passwords = {
  ["CoderHusk"] = "glitterbug1618";
  ["qu9090"] = "sciencenerd278";
  ["APUGLIFE"] = "mondays314"
}
local targetUser = "CoderHusk"

--Index method, we know the person who has it
print(passwords[targetUser])
--Class method, we don't know the person who has it
local function scan(tab, target)
  for i, _ in pairs(tab) do
    if tab[i] == targetPass then
      return tab[i]
    end
  end
end
print(scan(passwords, targetUser))
1 Like