Can anyone help me understand why I can't print tables from a dictionary, but only when I iterate?

local WeaponsPack = {
		["Class-D"] = {
			["Neutral"] = nil,
		},

		["Mobile Task Force"] = {
			["Rookie"] = {"ac1", "xp45"},
			["Enlist"] = {"ac1", "xp45"},
			["Sergeant"] = {"ac1", "xp45"},
		},

		["Rapid Response Team"] = {
			["Rookie"] = {"ac1", "xp45"},
			["Enlist"] = {"ac1", "xp45"},
			["Sergeant"] = {"ac1", "xp45"},
		},
	}

	--METHOD 1 OUTPUT  :  {} table w/nothing
	print(WeaponsPack["Class-D"])
	
        --METHOD 2 OUTPUT : {} table with all the index and values
	for i, atable in pairs(WeaponsPack) do
	    print(atable)
	end

As you can see, method 1 grabs the key of [‘Class-D’] with the output only coming out as a blank table.

However, method 2 iterates through the entire dictionary and is able to print the full table.

Theoretically, method 1 should work since in this example, calling the key from the dictionary works as intended.

local WeaponsPack2 = {
	['Class-D'] = "Hello world"
}

print(WeaponsPack2["Class-D"]) --output: "Hello world"

Why is that I can’t call tables from a dictionary? This thing has been quite bothersome and has taken up a lot of time trying to resolve as I’m just trying to code my project and finish stuff up. Is there a better method to go about dictionary/table stacking than this?

1 Like

The only value in the table is nil, so it won’t be displayed.

You indeed can get these values inside the dictionary by just indexing them, but you’re misunderstanding the real problem.

Your method 1 is indexing a table which does only have an index with a nil value, so for the script, this value doesn’t exist. With your method 2 is actually happening the exact same thing, but is printing out the other two because they have actual values.

If you change the nil value for something like “Hello world!” it will print the table as expected.

local WeaponsPack = {
	["Class-D"] = {
		["Neutral"] = "Hello world!",
	},
}

print(WeaponsPack["Class-D"])
1 Like
local WeaponsPack = {
	["Class-D"] = {
		["Neutral"] = nil,
	},

	["Mobile Task Force"] = {
		["Rookie"] = {"ac1", "xp45"},
		["Enlist"] = {"ac1", "xp45"},
		["Sergeant"] = {"ac1", "xp45"},
	},

	["Rapid Response Team"] = {
		["Rookie"] = {"ac1", "xp45"},
		["Enlist"] = {"ac1", "xp45"},
		["Sergeant"] = {"ac1", "xp45"},
	},
}

--METHOD 1 OUTPUT  :  {} table w/nothing
print(WeaponsPack["Class-D"]) -- Returns nothing because is nil.

--METHOD 2 OUTPUT : {} table with all the index and values
for i, atable in WeaponsPack do -- Do not use pairs for tables in script.
	print(atable)
end

I hope this helps you

Result:
image
For ignore the nil things you can do a
if #atable < 1 then print("table ignored cuz is empty") continue end. (not sure if this will works)

image
after you may get the values per table inside a table.

Thanks, I can’t believe such a simple thing was misunderstood!

But I kid you not, and no one would believe me even if I said so-- the same blank table output happened even though the key was [“Mobile Task Force”], and not [“Class-D”]. Now it mysteriously works, and [“Mobile Task Force”] prints the entire table.>!? It was not working earlier…

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