Referencing data in Array

Hi,

I am just leaning arrays and having issues referencing the data in the array below.

The second test2 below errors not being able to find Description.

Is my code wrong or am I doing the array wrong?

-- Works
print("test1 " .. tostring(QuestData.Q1.Description))
-- Does not work
print("test2 " .. tostring(QuestData["Q" .. playerQuests[0].Description]))

local questData  = {
		["Q1"] = {
			["Level"] = 0, 
			["NeededProgress"] = 10,
			["RewardType"] = "Gold",
			["RewardAmount"] = 100, 
			["Description"] = "Collect 3 Apples",
			["Chat1"] = "Can you do my quest",
			["Chat2"] = "Find three Apples",
			["Chat3"] = "I will reward you with 20 gold"
		},
		["Q2"] = {
			["Level"] = 0,
			["NeededProgress"] = 20,
			["RewardType"] = "Exp",
			["RewardAmount"] = 50,
			["Description"] = "Collect 3 Strawberries",
			["Chat1"] = "Can you do my quest",
			["Chat2"] = "Find three Strawberries",
			["Chat3"] = "I will reward you with 20 gold"
		},
	}

on test two it doesn’t work because you are looking for a key in the QuestData called “Q” plus whatever playerQuests[0].Description gives

2 periods means Concatenation

Hi,

Thanks, playerQuests[0] is 1 so should be QuestData.Q1.Description

Thanks

so do

print("test2 " .. tostring(QuestData["Q" .. playerQuests[0]]['Description']))
1 Like

Thanks that worked I did not think about the single quotes.