Saving Stats with DataStore2 (DEPRECATED)

Hello there! From searching around on the DevForums, I’ve haven’t really seen anybody talk about saving tables/dictionaries using DataStore2. I’ve also seen many people confused on how to do it so I thought it would be a good idea to make a tutorial on it.

(You might notice this is similar to this post VVV)
Credits to have this come up to attention: How to save Dictionary using DataStore2? - #13 by ForeverHD

(NOTE: This can easily be somewhat translated over to normal datastores as long as you have the understand of how it works)

First off, you’ll need to have the DataStore2 module, if you haven’t already, get the model here:

If you have gotten it already but can’t find it, this command will help you. (It will automatically insert it to workspace when you enter this into the command bar)

game.InsertService:LoadAsset(1936396537).MainModule.Parent = workspace

If you would like more information about DataStore2, you can refer to the original post: How to use DataStore2 - Data Store caching and data loss prevention

Anyways, lets get started with constructing the script and necessitates.

First, you’ll need to insert that DataStore2 module into ServerScriptService

Next, insert a normal script into ServerScriptService as well then open it. If you would like, you can name the module to anything else and the script, just keep in mind what the module’s name is.

Now we create the variables, you’ll want to start off with requiring the DataStore2 Module like this:

local DataStore2 = require(game:GetService("ServerScriptService").TheDataStore2ModulesName) -- Require the DataStore Module using the name

Next, you can create the key by making it a variable (Makes it easier to refer back to rather than having to change it for every single one if you plan on removing all data)

The main key can be named anything you like:

local MainKey = "HelloWorld!" -- Your Key's Name

Now you’ll want to combine other keys, allowing you to essentially save it into one large table by just calling the main key. Heres how you’ll do it:

DataStore2.Combine(MainKey, "Stats", "Achievements")

If you want to remove everybody’s data from the game, all you’ll need to do is change the key’s name to something else.

Now we’ll have to create the table:

local function CreateDataTable() -- function you'll call when you want to get the table
	local PlayerData = {
		Stats = {
			["Stage"] = 1;
			["Gems"] = 0;
		};
		Achievements = {
			["PlayedFor5Minutes"] = false;
			["TouchedAButton"] = false;
		};
	}
	return PlayerData -- Returns the table when function is called
end

The table will be later called by calling “CreateDataTable()”, essentially returning us the table with the default values and data.

Now we get onto the main part of saving and loading the player’s data.

game:GetService("Players").PlayerAdded:Connect(function(plr)
	local PlayerData = DataStore2(Key, plr):Get(CreateDataTable()) -- GetPlayersDataTable
-- Keep Continuing on

Now its time to create the stats/folders, if you want to show the player the other player’s stats, you’ll need to create a folder with the specific name of “leaderstats” otherwise, it won’t work.

You can create the folder and stats like this (DONT PARENT THEM YET):

local LS = Instance.new("Folder")
LS.Name = "leaderstats"

local Stat = Instance.new("IntValue")
Stats.Name = "Stage"

Now you can create the seperate keys/datastores variables that’ll be connected to the main key for more ease in development like so:

local AchievementsData = DataStore2("Achievements", plr)
local StatsData = DataStore2("Stats", plr)

Next, you want to update/get their stats to the player’s current data (If they even have any). If they don’t have data, it’ll automatically update their stats to correspond with the table’s stats, essentially the default values.
Heres how it can be done:

local function UpdateAllStats(UpdatedStats)
	Stat.Value = DataStore2(Key, plr):Get(UpdatedStats)["Stats"]["Stage"] -- Sets value you've made to correspond with data table
end

Now you’ll need to call the functions in order to have it work so:

UpdateAllStats(PlayerData) -- Calls the function and updates player data to appropriate values
DataStore2(Key, plr):OnUpdate(UpdateAllStats) -- Calls when updated

Now you can begin parenting them since we’ve already set their values and appropriate info to them.

LS.Parent = plr -- Sets to player
Stat.Parent = LS -- Sets to leaderstats to be shown

Now lets get to the part of changing and setting the data:

Local Achievements = AchievementsData:Get() -- Gets achievements data
Achievements.TouchedAButton = true -- Changes the data
AchievementsData:Set(Achievements) -- Sets the datastore with the data you've just changed

Now I’ll give you an example of how this can be done when using Touched events, Remote Events, etc:

RemoteEvent.OnServerEvent:Connect(function(ThePlayer)
	local Achievements = DataStore2("Achievements", ThePlayer):Get() -- Gets Achievements data from player
	Achievements.TouchedAButton = true -- Changes the data
	DataStore2("Achievements", ThePlayer):Set(Achievements ) -- Sets the changed value
end)

As you can see, we’re getting the player from the remote event and using it to change that certain player’s certain data. You can also do this with touched events, remote functions, and so on.

There are many other ways this can be applied to, as long as you can get the player, you’re able to change their data and other things.

NOTE: Make sure you enabled: “Enabled Studio Acess to API Services” and insert a boolvalue called SaveInStudio in ServerStorage to true to test if your datastore saves!

If there are any mistakes you notice, please comment them below, thank you for reading this and have a nice day! :slight_smile:

46 Likes

This topic was automatically closed after 1 minute. New replies are no longer allowed.