How do you save Tables to datastore?

I made an inventory GUI so now i just need to make it save each player’s inventory when they get a new item.
if you can’t just save a straight table to datastore then how would you save an inventory with many items in it?
All help is appreciated.

18 Likes

You can save data in a table using GlobalDataStore:SetAsync(), not sure if I’m just interpreting the question wrong?

3 Likes

i was hearing that you can’t just save a table by itself but you need to do it in a different way

if you can just save a table to datastore then that would make it much easier to make an inventory.

3 Likes

You may have heard that you’d need to run HttpService:JSONEncode() on it to turn the table into a string but IIRC it is completely unnecessary as Roblox turns the table into a string with or without the JSONEncode function. Summed up it’s just repeating a function that Roblox is already running and a general waste of time.

tl;dr yes you can use :SetAsync() without doing anything to the table

9 Likes

You can save tables into data store my setting them to a specific key within the data store.

local Inventory = { -- Example Player Inventory
   ["Item"] = 101,
]

game:GetService("Players").PlayerRemoving:Connect(function(Player) -- Checking if player is leaving game
   local Success, Error = pcall(function()
      return game:GetService("DataStoreService"):GetDataStore("DATA_STORE_NAME_HERE"):SetAsync(Player.UserId.."Inventory", Inventory)
      -- Setting the data for inventory for the player
   end)
   if Error then
      warn(Error) -- Showing there was error (Can also keep in log to fix)
   end
end)

This is an example version, I did real quick but the key aspects to see is that you are able to save tables as keys in a data store. In an optimal method, you should use :UdateAsync() to set the data instead of :SetAsync() in order to prevent some chances of data loss. Hope this helped :smile:

50 Likes

So could i save a :GetChildren() table to datastore?
becuase it would be easy to just save Inventory:GetChildren()

1 Like

No because :GetChildren returns a table of objects within an instance. I suggest instead putting all the values with their names in a table and saving that table. So for example:

-- Inside the player removing
local InventorySaveTable = {}

for i, v in next, InventoryFolder:GetChildren() do
   InventorySaveTable[v.Name] = v.Value -- Assuming all that you want to save are object values
end

 -- Continue onto to saving the data

This is a simple concept of it but I hope you see what I am trying to relay.

9 Likes

How will I get async so it can be stored in a string value inside ServerStorage? I also want to set it from the same string value. I think I don’t know how to store tables in a string value and vice versa.

	local rCodes = Instance.new("StringValue",playerData)
	rCodes.Name = "RCodes"
	rCodes.Value = "{}"

	pcall(function()
		rCodes.Value = DataStores:GetAsync(player.UserId.."-RC") or "{}"
	end)
--This is my code. I don't know how to fix it, it crashes datastores without error.
1 Like

How can I save this?

local Ranks = {
	Owners = {

		"AEW745";
		"Player1";

		Owner = true;
		CanBan = true;
		CanKick = true;
		CanUnBan = true;
		PunishButtonVisible = true;
	};

	HeadAdmins = {

		Owner = false;
		CanBan = true;
		CanKick = true;
		CanUnBan = false;
		PunishButtonVisible = true;
	};


	Admins = {




		Owner = false;
		CanBan = false;
		CanKick = true;
		CanUnBan = false;
		PunishButtonVisible = true;
	};

	Mods = {



		Owner = false;
		CanBan = false;
		CanKick = false;
		CanUnBan = false;
		PunishButtonVisible = false;
	};
}

return Ranks

refrain from necro posting. But, something like this should work.

-- Variables
local DS = game:GetService('DataStoreService')
local playerData = DS:GetDataStore('playerData')

local Ranks = {...} -- Data

playerData:SetAsync(key, Ranks) -- replace key with player key (E.x player.UserId..'Rank')
-- Use these 2 lines when you want to save the Data. (E.x PlayerRemoving)
1 Like

What is necro posting? Can you please explain that?

1 Like

its when you go into really old (already solved) threads and post to bring it back to the public’s attention. Making a new thread with your question is a better alternative.

Well, I didn’t want to make a new thread because I didn’t want to make duplicate topics even though none of them answer my question it might be seen as a duplicate. Therefore, I posted my question here to avoid ending up with duplicate topics or topics similar. My question is similar but has a lot of differences.

9 Likes

Sorry to bump this again everyone, but how would I use GetAsync to retrieve specific values?

My code:

Saving:

local plrValues = {
		["Checks"] = checks,
		["Iron"] = iron,
		["Silicon"] = silicon,
	}
ds:SetAsync(player.UserId .. "PlrValues", plrValues)

Getting:

local plrValues = ds:GetAsync(player.UserId .. "PlrValues")

Then what? How do a get a value out of the table?

local plrValues = ds:GetAsync(player.UserId, "PlrValues").Iron -- returns the Iron since the GetAsync returns the table you set previously
2 Likes

why is it necessary to do Http:JSONEncode ???

Now that I look back, it isn’t necessary or required. Not sure why I added that part. I’ll edit my previous post and correct my mistake.