Is it possible to create a Table and save/Load it?

So I saw multiple Threads about Datastore and about saving Tables but they all use the same table methode like

local table = {
 kills = 0,
 deaths = 0,
 cash = 0,
}

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

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?

Thanks in advanced!

5 Likes

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

Alright sweet but how would I save this table and load it in like I explained?

There has already been a topic on how to save tables, please check it out:

After reading trough it I noticed that they didnt’t even get the saving to work in this post.

2 Likes

For using data stores I would recommend reading the tutorial:

https://developer.roblox.com/en-us/articles/Saving-Player-Data

Another useful thread I found:

https://devforum.roblox.com/t/stop-using-setasync-to-save-player-data/276457

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
1 Like

Is there any way first to check if I saved the data?
Could I maybe print out the #Table? Because I am not very sure if it saves in the first place.

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)



Are you saving a StringValue? You can’t save a string value to a data store, but you can save a string to one as I have discovered in my topic here (How would you save a string value? - #2 by Ravenshield)

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.

edit: or if someone else comes in with an answer

Would it help alot if I share this place with your sir? Do you think you could solve it maybe then?

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

It’s only necessary if you’re working with large tables who can maybe go over the maxlimit. This is not going to happen in my game.

Mine is not even that big, it’s just one table for it and it’s still calling me out on UTF

Even if I encode it with json I still don’t properly know how to save/load it ^^

You know the basic of Datastore right? or just knew it

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

I was able to find a video to help you on this:

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.

2 Likes