Table index check

So basically this is driving me mad, I’m trying to see if the index of a table is equal to a certain name because I have a table set up like this inside of that table like so

["Uncomplete Missions"] = {

      ["NAME_OF_MISSION"] = {"string","string",1}


}

How would I check too see if the mission name is actually inside of the Uncompleted Missions dictionary

Didnt somebody give u an answer yesterday?

No this is not the same problem unfortunately

Oh Ok, have u tried Uncompleted Mission.NAME_OF_MISSION

Wait hold on let me try something

if whatever["Uncompleted Missions"]["Mission Name"] then -- whatever would check if uncompleted missions has a mission called “Mission Name” in it.

2 Likes

Yeah i tried this but its not printing it even though I know that its index is the mission name

EDIT : @rogchamp MissionContents[1] is the name of the mission, when I setup the data it looks like this

local UnCompleteMissions = _G.PlayerData[plr.UserId]["Inventory"]["UnComplete Missions"]
for idx, missions in pairs(UncompleteMissions) do
for missionname, missioncontents in pairs(missions) do
	if UncompleteMissions[missioncontents[1]] then
		print("working")
	end
end

The key should be wrapped in "

 ["name"] = {}

image

Then you would index the table as such

 local missions = {
   ["mission1"] = {}
 }

 if missions["mission1"] then
      --- do whatever
 end

NAME_OF_MISSION is an array, you can’t index it like like Dictoinary.Array, you will only get the array’s memory address.
You need to loop through it.

Well thats what Im doing right here, but it’s returning nil
I’ve print out missioncontents[1] and it gives me the mission name, so Im not sure why its returning nil

for idx, missions in pairs(UncompleteMissions) do
	for missionname, missioncontents in pairs(missions) do
		if UncompleteMissions[missioncontents[1]] then
			print("Working")
		end
	end

Uncomplete Missions is a dictionary, NAME_OF_MISSION is an array inside the dictionary.

Also, use ipairs over pairs() when working with arrays as that is faster.

You don’t even need two loops, here’s an example:

local TestDic = {
	Array1 = {
		'hello', 'test', '123'
	}
}

for _, value in ipairs(TestDic.Array1) do
	print(value)
end
1 Like

If you know what the values are inside of the dictionary or if you know what you’re looking for then you do not need a for loop at all.

1 Like

Only if you know what missions. You have more control when looping through.

Yeah thats a great visual example but the thing is I’m trying to find the name of the mission which is the key and if its there than like do stuff but its printing nil

Try to example, works fine for me.

You will have to loop through in that case as Syclya has shown.

There is no key/index in an array, only value.

Hello = Test
is basically
index = key

Hence, only dictionaries has a index/key.

Yeah Im looping through and I find the missionname which is the key but Im trying to check if the key of the mission is actually there

I already provided you with an example, I think you are confusing yourself a little here.