Pass table root trough a function with one value

Hey, I have this table:
[example]

local a = {
    b = {
        c = {
             d  ={
}
}
}
}

and this function

function DataStoreModule:setData(key,data,value)
 --key would mean b in this example
	a[key][data] = value
end

I want to set Test, that should be located under c to false
Test = false
Normally this would work
a[key(means b)][c][d][Test] = false (value)
but I can only pass data trough the function[ function DataStoreModule:setData(key,data,value)]
How can I pass that [[c][d][Test]] trough data and how can I place data so this [a[key(means b)][c][d][Test] = false (value)] works?

Datastore:setData(b,[idk],false)

Thank you for your help

could you send the :setData method in the module please? Thanks

You mean this? Or

this?
Or what do you mean?

My bad I’m blind, could be missing something but I believe it’s not possible to do it that way, currently very low on battery so I’ll come back later and try to help more if You still haven’t found a solution

You can modify the function to your needs. Also your current explanation of the problem is pretty much an XY problem (XY problem - Wikipedia). You’d typically be better off showing the actual code and explaining the actual use for what you’re trying to do, instead of rounding the problem up to what you think it is.

function DataStoreModule:setData(key,data,value)
	Data[key][data] = value
	DataStoreModule.DataChangedEvent:Fire()
	return true
end

This function

This code should call it
DataStore:setData(key)--,["inventory"]["Shop"][tostring(itemCategory)][item],false)
This is the table

local addData = {
	dataVersion = DataStoreVersion,
	gems = 0,
	inventory = {
		Boxes = {
			Textures = {
			},
			Guns = {
				
			}
		},
		Shop = {
			Accessory = {
			},
			Body = {
				 --Equipped , false = not equipped, nil = not in your inventory
			},
			Color = {
				
			},
			Tires = {
			}
		}
	}
}

I also have this function:

function DataStoreModule:getData(key)
	return table.clone(Data[key])
end

I dont want a function with 10 parameters only to go to this one point, one with 9,8,7,6,5,4,3,2 paramaters. One that only changes this data/value.

The code is not that much rounded.

The easiest solution would probably be to pass the entirety of the “b” table to be saved, so that you don’t have to worry about sorting through all of the nesting.

You could also use a sort of compounded key to search through the table using a method like this:

local a = {
	b = {
		c = {
			Test = true;
			d = {
				
			}
		}
	}
}

local function ChangeData(key, data, value)
	local tblToChange = a
	
	for _, nextKey in string.split(key, "_") do
		tblToChange = tblToChange[nextKey]
	end
	
	tblToChange[data] = value
end

ChangeData("b_c", "Test", false)
print(a)
1 Like

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