DS2Handler - An easier, simpler way to use DS2 (Open Source)

Introduction

Hello, fellow developers! Many of you may use DS2 (DataStore2 by @Kampfkarren ), and find that sometimes it can be a bit confusing. I myself was confused at first, but then I got used to how it works. Still, for many of you, it might be hard to use.

Well, I believe I have a solution. A solution to solve all of your combinings, and whatnot. A solution, that will save you countless hours of debugging things. A solution, so great that it will blow your socks off!

maybe that’s too much glorification

Behold, DS2Handler! A brand new module, that combines, saves, and does everything you would want to with DS2, all in 1 module! Made simple, easy to use, and very convenient! All you have to do is require the module, and use a few basic functions!

e.g

Usage

Let’s say we want to save some coins to a player. First, let’s get the module. Insert the module into ServerScriptService, then open it up, and do the following to set up the module. Now, upon opening the module, you should see something like this:

local Names,Defaults = {
},{}

This is where we’ll put the names of the values/datastores we want to save, under 1 datastore. Put simply, we want to store a bunch of data under a data key, kind of like storing a bunch of items into a safe. Now, there’s no need to overthink this, so all you need to do, is this:

local Names,Defaults = {
    {
	    combineKey = "mainData", -- The key that we are storing our data under
	    ["coins"] = 10, -- The name and default of the data,
    },
-- This is the format that you will be using to make a
--new datastore value, you can also add more by inserting a name and default
-- e.g  ["exp"] = 10

}, {}

Next, insert a script into ServerScriptService, and write the following into it:

local SSS = game:GetService("ServerScriptService") -- Define ServerScriptService
local DS2Handler = require(SSS:WaitForChild("DS2Handler")) -- Get the module

Now, whenever we want to add coins/whatever currency you want, all we have to do is run this:

DS2Handler(
    "Increment", -- action
    "coins", -- the name of the value we want to change
    player, -- the player 
    100 -- the amount, put a - behind it to subtract amounts. eg -100
)

Conclusion

That’s about it! Simple as that. Data will automatically save when the player leaves, so you don’t need to do anything with PlayerRemoving or anything like that. Listed below are all functions you can use. Please tell me what you think, report any bugs you might find, thank you and enjoy!

Documentation

DS2Handler(
   <string>  "Increment", -- the action
   <string>  "coins", -- name of the value/datastore we want to change
   <Player>  player, -- the player 
) -- Increments the value of the specified datastore


DS2Handler(
   <string>  "Get", -- the action
   <string>  "coins", -- name of the value/datastore we want to get
   <Player>  player, -- the player 
) -- Gets the value of the specified datastore

DS2Handler(
   <string>  "Set", -- the action
    <string> "coins", -- name of the value/datastore we want to set
   <Player>  player, -- the player 
    <number> 100 -- the value we want to set "coins" to
) -- Sets specified data store to specified value

DS2Handler(
    <string> "GetTable", -- action
    <string> "coins", -- name of the value/datastore we want to get
   <Player>  player, -- the player 
) -- Use this when you want to get the value of something that is stored in a table, aka {}

DS2Handler(
    <string> "OnUpdate", -- action
    <string> "coins", -- name of the value/datastore
    <Player> player, -- the player 
    <function> callback -- function called when datastore is updated, passes 1 argument which is the new value of the datastore
)

Links

Source: DS2Handler Source Code - Pastebin.com
Roblox File: DS2Handler - Roblox

9 Likes

A wrapper on a wrapper seems like a bit of an overkill, however, this may be helpful to newer developers. Nice guide.

6 Likes

I’ve heard duplication glitches can occur with DataStore2. I have yet to experience these glitches myself but ProfileService seems to be the new standard now, due to features like session locking.
Maybe you could have your wrapper support both ProfileService and DataStore2?

4 Likes

Sounds like an awesome suggestion. I’ll see what I can do, as I have not used profile service before.

1 Like

I’d suggest making a wrapper for ProfileService. I made one for personal use that mimics many of DS2’s features along with new ones I added but it’s not made for widespread use due to a lack of type checking, informative errors, etc, and I cba to document everything properly atm.

4 Likes

I am currently working on a wrapper for ProfileService, as requested! Please be patient, as I have not used PF before. I’ll try to get this out ASAP.

1 Like

Any updates on that? Or did you give up on it?

Ah, I’ve completely forgotten about it :sweat_smile:. I’ve recently started to take commissions, so I’ll work on this whenever I can :slightly_smiling_face:

1 Like

No problem. You do you, while that would be cool, it’s your project.

1 Like

How would I go about using this to migrate data from roblox datastore to to datastore2?

In a nutshell, you would first create a new set of data with this module, then check for old data, and port it over to DS2. e.g

local plr -- your player
local DS2Handler = require(game.ServerScriptService.DS2Handler) -- DS2Handler
local DSS = game:GetService("DataStoreService")
local oldDataStore = DSS:GetDataStore("oldDataStore") -- get your old datastore

local oldPlayerData = oldDataStore:GetAsync(plr.UserId) -- get player old data
DS2Handler( "Set", "coins" , plr, oldPlayerData.Coins ) -- updates data

Side note: Change the DataStore names and such accoording to your old data keys for this to work

1 Like

After the check, if anyone else wants to do the same it probably is a good idea to remove the old datastore so the check before seeing if it’s not nil will return nil and won’t overwrite new progress with old progress

1 Like

UPDATE ON A WRAPPER FOR ProfileService: Coming out soon, have some exciting features that’ll make using ProfileService much easier

(sorry for the long wait, I was really busy with my commissions

APRIL 30th UPDATE: releasing in a couple days hopefully, pretty much done
JULY 17th UPDATE: yeah I have motivational problems this might take a while sorry
OCT 16 UPDATE: so I released the module in august but forgot to update anyway here it is

1 Like