Help with iterating through tables?

I am wondering how I’m supposed to iterate through the player’s data table, and then creating a new variable if the player doesn’t have it in their table. Also, I am using Kampfkarren’s DataStore2 Module. I’ve looked through multiple sources in the dev forums, but couldn’t find a solution. I tried to use a for loop (for i, v in pairs), but after I became confused about what to do next. If you could help me find a solution to this, it would be gladly appreciated! Thank you!

Current DataStore2 Script:

-----------------------
-----------------------

warn(script.Name, "Executing.")

-----------------------
-----------------------

-- MODULES

local dataStoreModule 		 = require(script.DataStoreModule)

-- SERVICES

local playersService 		 = game:GetService("Players")
local replicatedStorage 	 = game:GetService("ReplicatedStorage")

-- REMOTES

local remotesFolder 		 = replicatedStorage:WaitForChild("Remotes")
local eventsFolder 			 = remotesFolder:WaitForChild("Events")

local updateEvent			 = eventsFolder:WaitForChild("UpdateEvent")

-----------------------
-----------------------

local function setupPlayerData()
	
	local playerData = {
		["Currency"] = {
			["Fossil"] = 0;
		};
		
		["Tyrannosaurus Rex"] = {
			["Owned"] = false;
			["Age"] = 0;
			["Health"] = 0;
			["Hunger"] = 0;
			["Thirst"] = 0;
		};
		
		["Allosaurus"] = {
			["Owned"] = false;
			["Age"] = 0;
			["Health"] = 0;
			["Hunger"] = 0;
			["Thirst"] = 0;
		};
	}
	return playerData
	
end

playersService.PlayerAdded:Connect(function(player)
	
	local playerDataStore = dataStoreModule("PlayerDataStore", player)
	local playerData = playerDataStore:Get(setupPlayerData())
	
	local function updateData()
		
		local updatedValue = playerData["Currency"]["Fossil"]
		
		playerDataStore:Set(playerData)
		updateEvent:FireClient(player, updatedValue)
	end
	
	updateData(playerData)
	playerDataStore:OnUpdate(updateData)
end)

I don’t know anything about DataStore2, but if we simplify this question as “How do I iterate through a table and add a value if it doesn’t exist?”, we can clear this up pretty easily:

  1. To check if a value exists in a dictionary (what you provided as an example is a dictionary). we don’t need to do any iteration, since we are indexing with strings.

Example:

local data = { -- an example of your player data table
    Coins = 12345; -- all we have is a Coins value
}

if not data.Tokens then -- if there's no Tokens value
    data.Tokens = 0 -- add the tokens value.
end
  1. If it’s an array (numberical indexed table) and you want to check if a value exists, you can use the new table.find function, which does all the iteration for you!:

Example:

local data = {"Cool Item"}; -- data[1] == "Cool Item"

if not table.find(data, "Cool Item") then -- there's no cool item.
    table.insert(data, "Cool Item") -- add "Cool Item" into the array
end

Hope this helps you understand a bit.

3 Likes

Oh… I see now. This response helped me quite a bit of understanding. Thank you!