· 6 years ago · Mar 13, 2020, 11:30 PM
1Hello 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.
2
3(You might notice this is similar to this post VVV)
4Credits to have this come up to attention: https://devforum.roblox.com/t/how-to-save-dictionary-using-datastore2/302422/13
5
6(NOTE: This can easily be somewhat translated over to normal datastores as long as you have the understand of how it works)
7
8First off, you’ll need to have the DataStore2 module, if you haven’t already, get the model here: https://www.roblox.com/library/1936396537/DataStore2-Data-Loss-Prevention-and-Caching
9
10DataStore2 - Data Loss Prevention and Caching
11Use DataStore2 - Data Loss Prevention and Caching and thousands of other assets to build an immersive game or experience. Select from a wide range of models, decals, meshes, plugins, or audio that help bring your imagination into reality.
12
13If 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)
14
15game.InsertService:LoadAsset(1936396537).MainModule.Parent = workspace
16If you would like more information about DataStore2, you can refer to the original post: https://devforum.roblox.com/t/how-to-use-datastore2-data-store-caching-and-data-loss-prevention/136317
17
18Anyways, lets get started with constructing the script and necessitates.
19
20First, you’ll need to insert that DataStore2 module into ServerScriptService
21
22Next, 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.
23
24Now we create the variables, you’ll want to start off with requiring the DataStore2 Module like this:
25
26local DataStore2 = require(game:GetService("ServerScriptService").TheDataStore2ModulesName) -- Require the DataStore Module using the name
27Next, 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)
28
29The main key can be named anything you like:
30
31local MainKey = "HelloWorld!" -- Your Key's Name
32Now 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:
33
34DataStore2.Combine(MainKey, "Stats", "Achievements")
35If 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.
36
37Now we’ll have to create the table:
38
39local function CreateDataTable() -- function you'll call when you want to get the table
40 local PlayerData = {
41 Stats = {
42 ["Stage"] = 1;
43 ["Gems"] = 0;
44 };
45 Achievements = {
46 ["PlayedFor5Minutes"] = false;
47 ["TouchedAButton"] = false;
48 };
49 }
50 return PlayerData -- Returns the table when function is called
51end
52The table will be later called by calling “CreateDataTable()”, essentially returning us the table with the default values and data.
53
54Now we get onto the main part of saving and loading the player’s data.
55
56game:GetService("Players").PlayerAdded:Connect(function(plr)
57 local PlayerData = DataStore2(Key, plr):Get(CreateDataTable()) -- GetPlayersDataTable
58-- Keep Continuing on
59Now 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.
60
61You can create the folder and stats like this (DONT PARENT THEM YET):
62
63local LS = Instance.new("Folder")
64LS.Name = "leaderstats"
65
66local Stat = Instance.new("IntValue")
67Stats.Name = "Stage"
68Now you can create the seperate keys/datastores variables that’ll be connected to the main key for more ease in development like so:
69
70local AchievementsData = DataStore2("Achievements", plr)
71local StatsData = DataStore2("Stats", plr)
72Next, 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.
73Heres how it can be done:
74
75local function UpdateAllStats(UpdatedStats)
76 Stat.Value = DataStore2(Key, plr):Get(UpdatedStats)["Stats"]["Stage"] -- Sets value you've made to correspond with data table
77end
78Now you’ll need to call the functions in order to have it work so:
79
80UpdateAllStats(PlayerData) -- Calls the function and updates player data to appropriate values
81DataStore2(Key, plr):OnUpdate(UpdateAllStats) -- Calls when updated
82Now you can begin parenting them since we’ve already set their values and appropriate info to them.
83
84LS.Parent = plr -- Sets to player
85Stat.Parent = LS -- Sets to leaderstats to be shown
86Now lets get to the part of changing and setting the data:
87
88Local Achievements = AchievementsData:Get() -- Gets achievements data
89Achievements.TouchedAButton = true -- Changes the data
90AchievementsData:Set(Achievements) -- Sets the datastore with the data you've just changed
91Now I’ll give you an example of how this can be done when using Touched events, Remote Events, etc:
92
93RemoteEvent.OnServerEvent:Connect(function(ThePlayer)
94 local Achievements = DataStore2("Achievements", ThePlayer):Get() -- Gets Achievements data from player
95 Achievements.TouchedAButton = true -- Changes the data
96 DataStore2("Achievements", ThePlayer):Set(Achievements ) -- Sets the changed value
97end)
98As 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.
99
100There 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.
101
102NOTE: 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!
103
104If there are any mistakes you notice, please comment them below, thank you for reading this and have a nice day! :)