How do I save a table from module script to datastore?

I’ve been having a lot of trouble trying to find a way to save a table from a module script into a server script datastore.

Is it even possible, if so how?

I wanna be able to save everything

Module Script with tables:

local SensitiveData = {
	Upgrades = {
		Chips = {
			Factory = true,
			Optimized = false,
			Enhanced = false,
			Recoded = false
		},
		Signals = {
			Entry = true,
			Mid = false,
			High = false,
			Deluxe = false
		}
	},
	Class = {Ascension = false, CovenCode = false, Druidex = false, Pyramystic = false}
}


return SensitiveData

DataStore Server Script:

local DataModule = require(script.Parent.DataTable)


game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"

	local aureus = Instance.new("NumberValue")
	aureus.Name = "Aureus"

	-- Move
	leaderstats.Parent = player
	aureus.Parent = leaderstats

	local data

	local success, errormessage = pcall(function()
		data = experienceStore:GetAsync("playerdata_"..player.UserId)
	end)

	if success then
		if data then
			local aureusValue = data.aureusValue or 0

			aureus.Value = aureusValue
			print("Loaded data")
		else
			aureus.Value = 0
		end
	else
		print("Error while data load")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local stats = player.leaderstats
	local aureus = stats.Aureus
	
	local data = {
		aureusValue = aureus.Value,
		upgrades = DataModule.Upgrades,
	}

	local success, errormessage = pcall(function()
		experienceStore:SetAsync("playerdata_"..player.UserId, data)
	end)

	if success then
		print("Player Data Saved")
	else
		print("Failed Save")
		warn(errormessage)
	end
end)

Okay what aare you trying to sav and got any code to show??

1 Like

I edited the topic and made it more clear

Probably yes, theres a Forum about this:

1 Like

here is your answer not mine btw. 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:

1 Like