Can i check if a dictionary has a key

I have two dictionaries, one is a template and the other needs to fit the template. I want to check on one of the dictionaries to see if it has all the keys the template has.

I tried making a for loop through all values in the template then checking if the dictionary to change doesn’t have the key but checking if the key = nil in the dictionary. However when i do this i get an error

“attempt to index nil with ‘key’”

Does anybody know how to check if a dictionary has a key and value…

thats what i tried and i got the error, i only got the error when it doesn’t exist

i tried it before and got the same results, yeah

Like what @Mixu_78 said, you can check if the key exists but indexing the table and checking if it is different from nil.

local foo = {  };

if foo[ "lol" ] ~= nil then --just in case you are using a boolean: false
       
end 

your error means you are indexing foo but it is nil.

local foo = nil;

if foo[ "lol" ] ~= nil then -- Error: attempt to index nil with ‘lol’
       
end 

Also you mentioned templates, the AeroFramework ArrayUtlity module contains a function which is most likely what you are looking for.

3 Likes

sorry to the both of you, i was using a datastore for this and when i retrieved data from a player that previously had no data the user didn’t have a dictionary to begin with it was just nil. I just had to declare it as a table first. Thanks for your help though

MARK AS SOLUTION!! This answered my question either way

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.