But how would I go about saving a more structured table? Basicly I am doing a inventory system and it’s fully working right now. I am storing all the items the players has in a folder in ServerStorage. Each Players has his own folder in there. I am storing each item the player has as a stringvalue with the itemname and another IntValue inside the stringvalue for the amount.
So what I was plan on doing is create a table inside the Leaderboard script and save this table.
I was thinking about maybe doing something like this?
function NewDataTable()
local inv = {}
for i, v in pairs(game.ServerStorage.Inventory:FindFirstChild(player.Name):GetChildren()) do
if v:IsA("StringValue") then
local vprop = {i,v.Name,v.Amount.Value}
table.insert(inv,vprop)
end
end
return inv
end
Would this work somehow? And how would I use this table to save it and actually load it?
So If the player joins the server it creates a folder in serverstorage and just creates values from the table that saved. Is that possible?
That method should work, but I would change the table structure a little:
for i, v in pairs(game.ServerStorage.Inventory:FindFirstChild(player.Name):GetChildren()) do
if v:IsA("StringValue") then
-- the order of the children in the folder isn't really relevant, so you probably won't need to store the index.
-- you can use the name of the item as a key in the table, too. This could save space and make the table easier to read when you retrieve it from the data store.
inv[v.Name] = v.Amount.Value
end
end
You will also need a method of converting a table with your saved data back into your folder structure once it is retrieved. Something like:
function restore(folder, userData)
for key, amount in pairs(userData) do
local s = Instance.new("StringValue")
s.Name = key
s.Parent = folder
local a = Instance.new("IntValue")
a.Name = "Amount"
a.Value = amount
a.Parent = s
end
end
To add on and help you with this, you could use this as for saving models and this is something you could do:
local Models = game:GetService("ServerStorage"):WaitForChild("Models") -- Replace with correct path
local datastore = game:GetService("DataStoreService"):GetDataStore("ModelSave")
game.Players.PlayerAdded:Connect(function(plr)
local good, info = pcall(function()
return datastore:GetAsync(plr.UserId)
end)
if good then
for i,v in pairs(info) do
local model = Models[v]
--Load Model
end
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local Model = --Path to their Model
local InfoToSave = {}
for i,v in pairs(Model:GetChildren()) do
table.insert(InfoToSave, v.Name)
end
datastore:SetAsync(plr.UserId, InfoToSave)
end)
This may have bugs, but I believe it should be ok.
And yes you can check to see if the table has saved something by printing it in the load section of your data store. I am not too sure on checking the whole table b/c you can’t print a table in the output, you probably could put in a for loop to see what has saved in it though and print each thing individually.
Alright, I believe I am very close to get it working. I’ve got an error on this part of the code
local good, info = pcall(function()
return datastore:GetAsync(player.UserId)
end)
if good then
for i,v in pairs(info) do
restore(folder,player.UserId)
end
end
ERROR:
[ServerScriptService.MainLeaderboard:57: bad argument #1 (table expected, got nil)]
Ive tried to implement the restore function aswell , here is my full code:
local datastore = game:GetService("DataStoreService"):GetDataStore("InventorySave")
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder",game.ServerStorage.Inventory)
folder.Name = player.Name
local characterfolder = Instance.new("Folder",folder)
characterfolder.Name = "Character"
local shirtfolder = Instance.new("Folder",characterfolder)
shirtfolder.Name = "Shirts"
local pantsfolder = Instance.new("Folder",characterfolder)
pantsfolder.Name = "Pants"
local hatfolder = Instance.new("Folder",characterfolder)
hatfolder.Name = "Hats"
local weight = Instance.new("NumberValue",folder)
weight.Name = "Weight"
weight.Value = 0
local function NewDataTable()
local inv = {}
for i, v in pairs(game.ServerStorage.Inventory:FindFirstChild(player.Name):GetChildren()) do
if v:IsA("StringValue") then
local vprop = {i,v.Name,v.Amount.Value}
table.insert(inv,vprop)
end
end
return inv
end
--Load Data in
local function restore(folder, userData)
print("Trying to load it in")
for key, amount in pairs(userData) do
print("Zack")
local s = Instance.new("StringValue")
s.Name = key
s.Parent = folder
local a = Instance.new("IntValue")
a.Name = "Amount"
a.Value = amount
a.Parent = s
end
end
local good, info = pcall(function()
return datastore:GetAsync(player.UserId)
end)
if good then
for i,v in pairs(info) do
restore(folder,player.UserId)
end
end
game.Players.PlayerRemoving:Connect(function(player)
local InfoToSave = NewDataTable()
print(#InfoToSave)
datastore:SetAsync(player.UserId, InfoToSave)
end)
end)
I am not saving a stringvalue, I am creating a table with from the strinvalues. Somewhere at the start of this post I showed my hierarchy for storing the items. Im just creating a table and storing the stringvalue Names aswell as the Amount value.
Oh sorry about that misunderstanding, I need to get on studio for this to help see what exactly is going on. Something with your table is catching it as a nil value though. I will have more info on this later for you.
I tried saving Table before, You can try json to encode the Table and then Decode the table that way will make you be able to make Save/Load file like one of those RPG where you have save slot and stuff but mainly You can save table if you json them if you don’t json them they will error out about UTF limit
I am super ■■■■■■■■ when it comes to datastore. Not gonna lie. I know how to save simple strings and int’s etc. but not tables. I really don’t know why it would make such a diffrence
He has a script in the description as well, so you can look at it for more help. It seems as if your table wasn’t declared yet though. See if this video helps you with any of your problems.