How to get Name and value from table

I am trying to make a quest system in which there’s ordered goals and i want to know how i can get the name and value from the table for example

local Tutorial = {
	["Kill 5 Peasants"] = 5,
	
	}

I want to get the string “Kill 5 Peasants” and the value 5, how would i do that?

2 Likes

I’m slightly confused over your question. Can you elaborate?


The best guess I have here is…

-- next() global
print(next(Tutorial))

-- assigning variables from next()
local key, value = next(Tutorial)
print(key, value) -- Kill 5 Peasants and 5 is printed.

-- for loop
for key, value in next, Tutorial do
    print(key, value)
end
1 Like

the key, value = next(tutorial) thing is what i’m trying to achieve how would i make it correspond to a number though? for example stage 1 corresponds to the first key in the table which is to kill 5 peasants then stage 2 could correspond to the second key

So something like

local Tutorial = {
	["Kill 5 Peasants"] = 5,
	["Kill 5 Local Lords"] = 5,
	}
print(Tutorial[1]) -- returns kill 5 peasants
print(Tutorial[2]) -- returns kill 5 local lords

Maybe you should do a different table.

Tutorial = {
	[1] = {"Kill 5 Peasants", 5};
	[2] = {"Kill 5 Local Lords", 5};
}

print(Tutorial[1][1], Tutorial[1][2])
print(Tutorial[2][1], Tutorial[2][2])

Perhaps utilizing ipairs() which always does things in numerical order. It is almost like next() but with a few differences only.

7 Likes