Quick Data store question

Hi, I am a new scripter, and data stores aren’t really my strength so I am trying to improve and learn more about them, also I have question.

My goal: I am trying to figure out a good way to insure no data is lost.

Question:
Is it better to Save data in to separate data stores or one big datastore?

Or maybe a different way (I heard saving Data in a Table is a good way but no clue how to do so)

local WinsDataStore = DataStoreService:GetDataStore("WinsDataStore")
local KillsDataStore = DataStoreService:GetDataStore("KillsDataStore")
local WeaponsDataStore = DataStoreService:GetDataStore("WepsDataStore")
local EquippedDataStore = DataStoreService:GetDataStore("EquippedDataStore")
local CoinsDataStore = DataStoreService:GetDataStore("CoinsDataStore")

or

local MainPlayerDataStore = DataStoreService:GetDataStore("MainPlayerDataStore")
1 Like

I would recommend having a single datastore to handle data related to a specific topic. If you want to store info about a player, usch as their wins, kills, deaths, etc, it would be better to store them in 1 datastore as a Dictionary ot table if you prefer.

The table storing is basically just making a table before the player leaves containing information to store, example

local data = {
    player.leaderstats.Wins.Value,
    player.leaderstats.Deaths.Value
    --And so on
}

And then storing that in the datastore. The way you then retrieve certain information is by referencing the index. So if you wanted the wins, you get the data from the player’s key in the datastore, and just do Data[1], where Data is the variable that contains the data retrieved from GetAsync, reason it gets the wins because it’s the first thing in the table, and you’re getting the first thing

@HackItsGood Your example will not work, you’re creating a table and then trying to get a Dictionary key from it

3 Likes

Could give me an example for storing please?

I know I was typing in rush

sorry…

I think this should help you: :grinning:

1 Like

Basically lets assume you know the basics of datastoring, a simple example would be this

game:GetService("Players").PlayerRemoving:Connect(function(player)
	local stats = player.leaderstats
	local data = {
		stats.Wins.Value,
		stats.Deaths.Value,
		stats.Kills.Value
	}
	local success, result = pcall(function()
		DataStore:SetAsync(player.UserId, data)
	end)
	
	if success then
		print("Data saved successfully")
	else
		print("Error occured when saving")
		warn(result)
	end
end)

Gets the player leaderstats, and then makes a table of the things to save, then sets them in the Datastore into the player’s key with the UserId. It is pcalled so the script doesn’t halt if it errors, and then we do a bit of printing to see if it was successful or had an error

1 Like

Thank you this helped me a lot!

1 Like

I also recommend reading up on what @HackItsGood had mentioned since it also describes another important thing when datastoring, connecting the saving to a BindToClose, so if the server shuts down, it’ll still save the data.

You can also learn about how to make your code use UpdateAsync as it would be a more proper way of saving data, here’s a post to help you understand

Only thing I’d do different in the code in the post HackItsGod provided would be to coroutine the SaveData function when it has to go through all the players in the BindToClose

2 Likes