How do you save a table to a datastore?

I’ve been trying to save the list of owned cars that a player has into a datastore.
Here’s a screeenshot of the way the ownedCars folder looks:
image

I’ve never used a table other than just a regular array but I’m needing this one to save the car name and then information linked to each car like color and registration number.

I’ve not been getting any errors in the datastore script but it’s not saving the information. The datastore itself is fine as there is other values stored in it that save fine.

local ownedCarsTable = {}
local ownedCars = player.playerStats.ownedCars:GetChildren()
if ownedCars ~= nil then
    for i = 1, #ownedCars do
        ownedCarsTable = {
            vehicleName = ownedCars[i].Value,
            vehicleColor = ownedCars[i].CarColor.Value,
        }
    end
    myDataStore:SetAsync(player.UserId.."-ownedCars",ownedCarsTable)
end

Also this is the line I used to try save the table:
ownedCarsTable = myDataStore:GetAsync(player.UserId.."-ownedCars")
I know that the datastore works as I already have other stuff using it but I just can’t seem to get this to save. Thanks

You are overwriting ownedCarsTable each iteration. Instead, do table.insert(ownedCarsTable, {}) to add to the array.

1 Like

So, hm I have this module it might help you, like Very SPECIFICALLY.

You save data with SetAsync or UpdateAsync. Each iteration, you rewrite the whole table which is not what you want and is thus not working.

 ownedCarsTable = {}
 for i = 1, #ownedCars do
        ownedCarsTable[i] = ownedCars[i]
    end

In my testing it didn’t even save the 1 car I was trying to save leading me to believe that theres something else wrong. Here’s the whole script just with the other values removed for simplicity.

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("PointsDataStore")

game.Players.PlayerAdded:Connect(function(player)
	
	local statsFolder = Instance.new("Folder") --Main Stats Folder
	statsFolder.Name = "playerStats"
	statsFolder.Parent = player
	local carsFolder = Instance.new("Folder") --Cars folder
	carsFolder.Name = "ownedCars"
	carsFolder.Parent = statsFolder
	
	--there's normally stuff here that goes in the stats folder.
	
	--again, theres normally more here
	local ownedCarsTable
	
	local success, errormessage = pcall (function()
		--you get the idea - i've removed unrelated stuff from this script.
		ownedCarsTable = myDataStore:GetAsync(player.UserId.."-ownedCars")
	end)

	if success then
		--same again
		if ownedCarsTable ~= nil then
			for i = 1, #ownedCarsTable do
				local carMain = Instance.new("StringValue")
				carMain.Name = "playerCar"..i
				carMain.Value = ownedCarsTable[i].vehicleName
				carMain.Parent = carsFolder
				local carColor = Instance.new("Color3Value")
				carColor.Name = "CarColor"
				carColor.Value = ownedCarsTable[i].vehicleColor
				carColor.Parent = carMain
			end
		end
		print("Player data successfully recieved!")
	else
		print("Error while attempting to recieve player data.")
		warn(errormessage)
	end
	
end)



game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		--same again
		local ownedCarsTable = {}
		local ownedCars = player.playerStats.ownedCars:GetChildren()
		if ownedCars ~= nil then
			for i = 1, #ownedCars do
				ownedCarsTable = {
					vehicleName = ownedCars[i].Value,
					vehicleColor = ownedCars[i].CarColor.Value,
				}
			end
			myDataStore:SetAsync(player.UserId.."-ownedCars",ownedCarsTable)
		end
	end)
	
	if success then
		print("Player data successfully saved!")
	else
		print("There was an error while attempting to save player data.")
		warn(errormessage)
	end
	
end)

Thinking this through a bit more - the only reason I know it’s not working is because it’s not creating the string value when I rejoin the game so the issue might not be with saving but instead loading. I know the datastore itself should be fine because other data in it saves completely fine - it’s just the car stuff.

Table To Object COULD be useful for your uses. It’s a module I made, it can convert tables into objects, and folders. and vice-versa. Might wanna look into my profile.