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
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
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!
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.
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.
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
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.