DataStore2 Module trouble. What am I doing wrong?

The following script won’t work, what am I doing wrong? This currently uses the Kampfkarrens’s DataStore2 module, and I’m not sure if this is the correct way to use, the directory at the module page doesn’t explain much to me.

game.Players.PlayerAdded:Connect(function(Player)
    local LP = DataStore2("PointsSave", Player)
   local Points = Player["Player_Currency"]:FindFirstChild("Points")

    local function callRemote(value)
        print("Called")
       Points.Value = LP:Get(value)
    end

    callRemote(LP:Get(0))
    LP:OnUpdate(callRemote)

       Points.Changed:Connect(function(newVal)
         print("Changed")
          LP:Set(newVal)
        end)
end)

P.S the output won’t print “Changed” so I did try doing this the other way:

game.Players.PlayerAdded:Connect(function(Player)
    local LP = DataStore2("PointsSave", Player)

    local function callRemote(value)
        print("Called")
       Points.Value = LP:Get(value)
    end

    callRemote(LP:Get(0))
    LP:OnUpdate(callRemote)
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local LP = DataStore2("PointsSave_System", Player)
	local Points = Player["Player_Currency"]:FindFirstChild("Points")
	LP:Set(Points.Value)
end)

Does your new code work?

Nope, not at all. I changed the numbervalue from the server and it did not save once I left

Try using WaitForChild to wait for the Value and folder to load. It might not load as the player joins.

local Points = Player:WaitForChild("Player_Currency"):WaitForChild("Points")

I tried that as well, I’m guessing I’m operating with the API incorrectly

If you can’t figure out the module, you could always make your own datastore methods. It is actually pretty easy to save and get data.

-Initialize datastore object to the selected datastore

function save(player, data)
-implementation

function getdata(player)
-implementation

I was gonna do that, but I noticed that this module is widely used, and handles a lot of the dirty work for you.

Instead of using :Set() for each time points is changed, use :Increment(), for each time points is expected to change, so you know what will save and what won’t save.

This tutorial goes over on the basics of DataStore2, as well as using Increment.

I did take a look at this as well, however that doesn’t answer my question, it covers how to save the data as something is fired, but in my variant I want the data to save as the player leaves the game. I tried using increment for that as well however, I’m still getting the same value that I spawned with

Like I said earlier, you are not limited to this module for data storing. Sure, it probably makes data storing a little easier, but there is so much more you can do with custom data storing. Just a thought :slight_smile:

This might be unlikely, but they might have edited the module since the video tutorial. I am not sure about this though.

1 Like

Do you by any chance know why it wont print “Changed” in the output when the changed event is fired?

The points.Changed event is only called when the points value is changed.

I have 2 possible solutions:

-Make sure that the points variable ~= nil

-Test the code by changing your points.Value value

I got those down, are there any limitations to where you could use that numberValue.Changed event?

1 Like

Int Value : Changed Event

Fired whenever the IntValue/Value of the IntValue is changed.

Technically, it should work.

Instead of updating the data store every time your points value changes, you should update it once a minute or whenever the player leaves (whichever one comes first).

Your code is going to cause an infinite loop, first of all.

There are two scenarios:

  • Data is set on its own

    • OnUpdate is called, which sets the leaderstat
    • Leaderstat is changed, which sets the data
    • Repeat
  • Leaderstat is changed on its own

    • Leaderstat is changed, which sets the data
    • OnUpdate is called, which sets the leaderstat
    • Repeat

Your PlayerAdded connection might not even be ran at all (a Studio issue). Consider instead:

local Players = game:GetService("Players")

local function playerAdded(player)
    local pointsStore = DataStore2("PointsSave", player)
    local points = Player:WaitForChild("Player_Currency"):WaitForChild("Points")

    local function callback(value)
        print("callback")
        points.Value = value
    end

    callback(pointsStore:Get(0))
    pointsStore:OnUpdate(callRemote)
end)

for _, player in pairs(Players:GetPlayers()) do
    playerAdded(player)
end

Players.PlayerAdded:connect(playerAdded)

Then whenever you need to give them points, in the script that’s doing the work, just:

local pointsStore = DataStore2("PointsSave", player)
pointsStore:Increment(points, 0)

…instead of changing the leaderstat manually.

1 Like

Is there no way to save the stat when the NumberValue has been changed? It would look cleaner to have only one script to handle all the data storing. So say you would change the PointsValue in the folder and as soon as that change happens it would save the new number in the DS.

Swap the :OnUpdate for points.Changed, and use :Set there, but don’t do both.