Multiple questions related to tables

Multiple questions here:

Question 1

Say I have a table structured like this:

local missions = {
	["SAMPLE MISSION"] = {
		MissionName = "Sample Mission",
		MissionObjective = "Placeholder",
		MissionType = "MAIN QUEST"
	},
	["SAMPLE MISSION 2"] = {
		MissionName = "Sample Mission 2",
		MissionObjective = "Another placeholder",
		MissionType = "SIDE QUEST"
	},
}

How can I make it so that a for loop only loops through the sample missions, and not their counterparts?

Question 2

How could I save a complex table like the one in Question 1 inside a Datastore?
(NOTE: I COULD NOT FIND THE SOLUTION TO MY PROBLEM IN THE DEVFORUM.)

for i, sample in pairs(missions) do
	for b, missioninfo in pairs(sample) do
		--do stuff here
	end
end

The above post answers question 1

To answer question 2:
To save a dictionary (what you referred to as a ‘complex table’), you need to change it into a string. This can easily be achieved using JSONEncode

local Table = {
    ["Hello"] = "Goodbye",
    ["Goodbye"] = "Hello"
}

local Encoded = game.HTTPService:JSONEncode(Table)
game.DatastoreService:GetDataStore("Table"):SetAsync("KEY", Encoded)

just remember to decode it when you get it again

local Encoded = game.DatastoreService:GetDataStore("Table"):GetAsync("KEY")
local Decoded = game.HTTPService:JSONEncode(Encoded)

-- Decoded will have basically the same value as Table did in the previous lot of code

Thanks to both @boterflic5 and @SeargentAUS! Can’t give a solution to both of them as they both helped me equally!

1 Like

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