Adding Values into Table

So I am trying to create a multidimensional Table which I can save/load.
The saving and loading itself works but I am having problems creating the actuall Table.

I have this Hierarchy which I want to form into a table.

Right now my table has all the propertys and the StoredCars inside of them. But what I am missing is the value inside each car. I don’t know how to store the value from the car stringvalues and load them back in later.

I have this methode to load the Table structure back in.

game.Players.PlayerAdded:Connect(function(player)
	local newData = DataStore:GetAsync(player.UserId)

	local plrfolder = Instance.new("Folder",player)
	plrfolder.Name = "DataStore"
	
	local propfolder = Instance.new("Folder",plrfolder)
	propfolder.Name = "Properties"
	
	if newData then
		print("Found data")

		for property, cars in pairs(newData) do
			local prop = Instance.new("StringValue")
			prop.Name = property
			
			local fold = Instance.new("Folder")
			fold.Name = "StoredCars"
			fold.Parent = prop
	
			for _, car in ipairs(cars) do
				local val = Instance.new("StringValue")
				val.Name = car
				val.Parent = fold
			end
	
			-- Parent after all properties and children have been added for performance reasons.
			prop.Parent = player.DataStore.Properties
		end
	end
end)

So how would I actually store the value in the inv table and load it back in, into the car value?

This is my store table methode btw.

function saveInventory(Player)
	local inv = {}

	for _, c in ipairs(game.Players[Player.Name].DataStore.Properties:GetChildren()) do
		
		inv[c.Name] = {}
		
		for _, d in ipairs(c.StoredCars:GetChildren()) do
			if d:IsA("StringValue") then
				table.insert(inv[c.Name], d.Name)	
			end
		end
	end

	pcall(function()
		DataStore:SetAsync(Player.UserId, inv)
	end)
end

Thanks!

1 Like
for carnumber,value in pairs(cars) do
print(carnumber.." = "..value)
end

output:
car00 = ValueHere

When you :GetAsync(),
Just set the variable/table to the saveddata/savedtable given
like this

local cartable = {}
local data
local success, errormessage = pcall(function()
data = yourdatastore:GetAsync("keys-"..userid) --whatever ur key is
end)
if success then
cartable = data
else player:Kick("You have failed to load your data")
end

I think you need to set the Value property for the StringValue objects by doing instance_name.Value = …

Wait how is that supposed to work? this is totaly different from what I have.
After trying your stuff out it’s not doing what I want.

for carnumber,value in pairs(cars) do
   print(carnumber.." = "..value)
end

Carnumber should be car00 but it’s not. its 0

because your solution is going through an endless path.
go use this easy method
its god
im sure
im bad
dont follow
im bad


See? The cars are now numbered and the value is the car name?

EDIT: Like I said, I am not storing the car value yet in my table. I need help with that.
Right now the table only exists of property and the correct stored cars but NOT the value yet!

Your data saved will return a table right? do a loop
for i,v in pairs(GivenSavedData:GetChildren()) do
and use v because that will be the value saved

Like I already said twice now. I am not storing the value yet in the table so its impossible to get that data from the table.

I need help with storing the value in the table.

wait what, but im teaching you how to store the value in the table by getting the data from the table
you have to get data if you want to store them
what i dont understand
pls elaborate
data is important.
without data, you can’t store.

Please read my post again.

I have the setup in my playerfolder and I want to create a table with that stuff.

All the stuff in properties is going to be formed in a table with that line of code

function saveInventory(Player)
	local inv = {}

	for _, c in ipairs(game.Players[Player.Name].DataStore.Properties:GetChildren()) do
		
		inv[c.Name] = {}
		
		for _, d in ipairs(c.StoredCars:GetChildren()) do
			if d:IsA("StringValue") then
				table.insert(inv[c.Name], d.Name)	
			end
		end
	end

	pcall(function()
		DataStore:SetAsync(Player.UserId, inv)
	end)
end

I store the Propertys and the cars in the table BUT NOT THE CAR VALUE YET.
And I want to store each carvalue aswell!
How would I do that?

Because after the table is done constructed, I can save the whole table and load it back in when the player joins and create the same hierarchy again.

Why not use something like this?
table.insert(inv[c.Name], {Name = d.Name, Value = d.Value})

Accessing it would be as simple as this:
inv[c.Name].Value

I think It’s misplacing the argument of table.insert()
this is an argument of table.insert():

table.insert(Table, PositionOfNumber, Value)

so It means you’re forgetting PositionOfNumber, which where that value contains.

function saveInventory(Player)
	local inv = {}

	for _, c in ipairs(game.Players[Player.Name].DataStore.Properties:GetChildren()) do
		
		inv[c.Name] = {}
		
		for i, d in ipairs(c.StoredCars:GetChildren()) do
			if d:IsA("StringValue") then
				table.insert(inv[c.Name], i, d.Name)	
			end
		end
	end

	pcall(function()
		DataStore:SetAsync(Player.UserId, inv)
	end)
end

Changes of script:

table.insert(inv[c.Name], d.Name) -- Before
table.insert(inv[c.Name], i, d.Name) -- After

let me know if I was wrong.

But that still only saves the car name not the value.
But I want the car name + the car value

game.Players.PlayerAdded:Connect(function(player)
	local newData = DataStore:GetAsync(player.UserId)

	local plrfolder = Instance.new("Folder",player)
	plrfolder.Name = "DataStore"
	
	local propfolder = Instance.new("Folder",plrfolder)
	propfolder.Name = "Properties"
	
	if newData then
		print("Found data")

		for property, cars in pairs(newData) do
			local prop = Instance.new("StringValue")
			prop.Name = property
			
			local fold = Instance.new("Folder")
			fold.Name = "StoredCars"
			fold.Parent = prop
	
			for _, car in ipairs(cars) do
				local val = Instance.new("StringValue")
				val.Name = car[1]
                                val.Value = car[2]
				val.Parent = fold
			end
	
			-- Parent after all properties and children have been added for performance reasons.
			prop.Parent = player.DataStore.Properties
		end
	end
end)
function saveInventory(Player)
	local inv = {}

	for _, c in ipairs(game.Players[Player.Name].DataStore.Properties:GetChildren()) do
		
		inv[c.Name] = {}
		
		for i, d in ipairs(c.StoredCars:GetChildren()) do
			if d:IsA("StringValue") then
				table.insert(inv[c.Name], i, {d.Name,d.Value})	
			end
		end
	end

	pcall(function()
		DataStore:SetAsync(Player.UserId, inv)
	end)
end

How about this one? adding value (table) saves 2 datas a once.

1 Like

Omg yes. Finally. Thanks so much sir.
Can’t believe this took so long.

2 Likes

@Mitumitu17YN Don’t use the parent argument of Instance.new. You got it at the bottom but the top half is still using it.

@OppositeFox431 Off-topic. Create a new thread for a new problem.

2 Likes

I almost copied a script from author, I just edited. you should say that to author.