How to loop trough nested tables

I’m busy with creating an system what only use one table and so on one data store and only save once on player leave but I come up to an error where it doesn’t set the default value on loading when data doesn’t exist

Also I just want it to loop trough the entire table and also check if the same exist into the data store and just keep the default table values when not yet exist

So template be set into the user id into the player data

Then I want it to loop trough the entire table and set valeus to the data store valeus elseif nil skip it and keep defould

But how I go about it with nested tables this is needed for if there’s new data

So now I do it like this but I want to load the entire table with the datastore and use default valued if it won’t exist


local sesionData = require(script.Parent.SesionData)

local DS = game:GetService("DataStoreService")
local PlrData = DS:GetDataStore("PlayerData_-1")

local Config = script.Parent.Configuration

local Tools = {
	[1] = "TestTool",
	[2] = "TestTool2",
}

local module = {
	load = function(userId)
		local data = PlrData:GetAsync(userId)
		if data == nil then
			PlrData:SetAsync(userId, sesionData.Template)
		end
		task.wait(0.1)
		local SavedData = PlrData:GetAsync(userId)
		sesionData.PlayerData[userId] = sesionData.Template
		warn(sesionData.PlayerData)
		task.wait(0.1)
		local inventory = sesionData.PlayerData[userId].Inventory
			warn(inventory)
		task.wait(0.1)
		for i = 1, Config:GetAttribute("AmounthTools") do
			
			local toolName = Tools[i]
			warn(toolName)
			
			if not SavedData.Inventory[toolName].Owned then
				-- Initialize with a default structure if it doesn't exist
				SavedData.Inventory[toolName].Owned = false
			end
			inventory[toolName].Owned = SavedData.Inventory[toolName].Owned
			
			print(sesionData.PlayerData[userId])
		end
		
	end,
}

return module
1 Like

The code you provided has the following errors and issues:

  1. script.Parent is not defined anywhere in the script. Make sure to define it before using it.
  2. In the loop, the Tools table indices are starting from 1 instead of 0. To fix this, update the loop condition to for i = 1, Config:GetAttribute("AmounthTools") do and change the Tools[i] reference to Tools[#Tools + 1 - i].
  3. The Config variable is not defined. Make sure to define it before using it.

Instead, use

local SesionData = require(script.Parent.SesionData)
local DS = game:GetService("DataStoreService")
local PlrData = DS:GetDataStore("PlayerData_-1")
local Config = script.Parent.Configuration

local Tools = {
    [#Tools] = "TestTool",
    [#Tools + 1] = "TestTool2",
}

local module = {
    load = function(userId)
        local data = PlrData:GetAsync(userId)
        if data == nil then
            PlrData:SetAsync(userId, SesionData.Template)
        end
        task.wait(0.1)
        local SavedData = PlrData:GetAsync(userId)
        SesionData.PlayerData[userId] = SesionData.Template
        warn(SesionData.PlayerData)
        task.wait(0.1)
        local inventory = SesionData.PlayerData[userId].Inventory
        warn(inventory)
        task.wait(0.1)
        for i = #Tools, 1, -1 do
            local toolName = Tools[i]
            warn(toolName)
            if not SavedData.Inventory[toolName].Owned then
                -- Initialize with a default structure if it doesn't exist
                SavedData.Inventory[toolName].Owned = false
            end
            inventory[toolName].Owned = SavedData.Inventory[toolName].Owned
            print(SesionData.PlayerData[userId])
        end
    end,
}

return module

1 Like

I just realized but this is so obviously chatgpt/other ais… wdym define script.Parent???

1 Like

Also just use a simple loop like this:

for key, value in Config do
	-- Key is whatever the thing is called (in this case it will just be numbers)
	-- Value is the value associated with the key
end

fr is answering on every post trying to get ‘solution marks’ lol (obviously ai)

ah yes let me define config and script.parent

ik what you mean and ignoring is a thing

So the key will be every part of the table it’s nested like

Local table = {
   Food = {
      Eggs = {
         Box = cardboard,
         Prize = 50,
     },
     Chocolate = {
         Box = paper,
         Prize = 60,
     }
}
}

The Databin the datastore can be different than into the template so it errors cuz it’s not yet into datastore

So you want to load from a datastore, and if it doesn’t contain an item thats in the table of defaults, load the default item anyway?

Yes it’s an nested table and I want it so that it not errors when there be data added

Do you want all leaf elements to be replaced or only down to a specific level? So if you load a table with Eggs that have a Box entry but no Prize, do you want to add the default Prize or just leave it?

If it doesn’t exist in the datastore table then use the default value

This will add any items not in “loaded” with the same chain of indices from “defaults”

local defaults = {
	Food = {
		Eggs = {
			Box = "cardboard",
			Prize = 50,
		},
		Chocolate = {
			Box = "paper",
			Prize = 60,
		}
	}
}

local loaded = {
	Food = {
		Eggs = {
			Box = "plastic",
			Prize = 20
		}
	},
	Slime = {
		Green = 20
	}
}

--Ensure table has all the indices in a defaults table
--If not, copy them from the defaults
function SupplyDefaults(loaded, defaults)
	for i, v in pairs(defaults) do
		local has = loaded[i]
		
		if type(v) == "table" then
			if not has then
				loaded[i] = {}
			end
			
			--Check sub-table
			SupplyDefaults(loaded[i], v)
		elseif not has then
			loaded[i] = defaults[i]
		else
			--Already has this value
		end
	end
	
	return loaded
end

loadedwdefault = SupplyDefaults(loaded, defaults)

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