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)
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
This might be unlikely, but they might have edited the module since the video tutorial. I am not sure about this though.
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)
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.