Saving & loading a lot of data; best practices?

So I’m looking to set up a data saving & loading system for essentially a player inventory system. In theory, each player will have an inventory full of weapons that they bought, and each weapon has its own upgrades. If there are 20 different weapons, and 10 upgrades per weapon, that’s over 200 possible weapons. The upgrades, being able to be added or removed based on the players preference, makes an unprecedented number of item combinations as well.

This isn’t even including cosmetic items & player stats. Theoretically, I need to be able to access the players data from both the server (datastore, other players, etc.) and the client (UI, shop, etc.)

download (11)
This is just a mockup I’ve made of what the data may look like inside the player. However, I have a couple of questions for you guys:

  1. Is this the most efficient way of storing data? If not, what is?
  2. What would the most efficient way of saving/loading a large amount of data like this be?
  3. If the most effective way of storing the data isn’t in the player like my screenshot, what would be the ideal method of accessing this information from the client & server?

Thanks in advance for any advice you guys can offer!

Hey, well, I don’t know if it’s the best practices, but, I used something similar, but exchange of creating a lot of values for the players, I used OOP!

local ToSave = {}
ToSave.__index = ToSave


function ToSave.new(plr)
	return setmetatable({Kills = 0, Deaths = 0, Assists = 0, Level = 0, Losses = 0, XP = 0, XPRequirement = 0, Wins = 0}, ToSave)
end


function ToSave:GiveKills()
	self.Kills = self.Kills + 1
end -- And you can do this,

-- And to save, you can do

function ToSave:ReturnInformation()
return {self.Kills, self.Deaths, self.Assists} --Etc...
end

return ToSave

and from other script you can do

local handler = require(modulewiththescriptIsentyou)
local DataStore = game:GetService("DataStoreService"):GetDataStore("Dat1a212221")
local SERVERSTATS = {}

game.Players.PlayerAdded:Connect(function(plr)
local Stats = handler.new(plr)
SERVERSTATS[plr.UserId] = Stats
	local success, data = pcall(function()
			return DataStore:GetAsync(Player.UserId)
		end)
if data then
Stats:GiveKills(data[1]) -- etc.. you will make a function on the module for all the stats, and give them points.
end
end)

game.Players.PlayerRemoving:Connect(function(plr)
local UserStats = SERVERSTATS[plr.UserId] 
local ToSave = UserStats:ReturnInformation() -- Returns a table with the stuff to save
DataStore:SetAsync(Player.UserId, ToSave)
end)

-- Let's for example, you want to make an event, that if you have certain xp then you can do something?

game.ReplicatedStorage.Event.OnServerEvent:Connect(function(Player)
local UserStats = SERVERSTATS[plr.UserId] 
local Return = UserStats:ReturnInformation()
if Return[1] >= 10 then -- If players has more than 10 kills then..
print("Player kills: "..Return[1])
end
end)
2 Likes

I’d use module scripts, they are way easier to navigate, as far as OOP goes, I wouldn’t unless your familiar with it. The wiki has a really good tutorial on how to set up a basic datastore module.

As far as best practices go, I use http:JsonEncode() and jsonDecode() and normally never touch the limit which is 260,000 characters.