Hello everyone.
I’ve made a simple script to save the points that a player has. But, they are not being saved and I don’t understand why.
Here is the part of my code that saves the value of the points the player has.
game.Players.PlayerRemoving:Connect(function(player)
local ID = pointsName .. "-" .. player.UserId -- pointsName = "Points"
DataStore:SetAsync(ID, player.leaderstats.Points.Value) -- I also tried with player.leaderstats[pointsName].Value
print(player.Name .. "'s points were saved.")
end)
I’ve found that some things get removed before that function is called, so you might try caching their points value and then just firing a function to save that cached value.
local pointsName = "Points"
local DataStore = game:GetService("DataStoreService"):GetDataStore("PointsStore_db1")
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = player
local points = Instance.new("IntValue")
points.Name = pointsName
points.Parent = folder
local ID = pointsName.."-"..player.UserId
local savedData = nil
pcall(function()
savedData = DataStore:GetAsync(ID)
end)
if savedData ~= nil then
points.Value = savedData
print("Player currency data loaded." .. " | JNS |")
else
print("New player added to the currency system" .. " | JNS |")
end
end)
I’d highly recommend looking into that caching method I was talking about in my previous post, because I have a feeling that the leaderstats is just getting deleted before the save can occur.
Because after changing my points, and reconnecting, the value returns to the previous one (the value before changing it).
Example:
I currently have 100 points. – I’ve put them before. And at that time they were saved.
I change them to 50 using the console in Studio.
I disconnect.
I reconnect and the value of the points is 100.
Wait are studio APIs enabled under GameSettings?
Are you setting the data in the client (if accurate play solo is on)? Setting the data in the server is the only way to actually save the data.
But if I stop saving player points when disconnected, I should update the DataStore every time the player points value changes. (When he buys / sells something). Because updating it for a certain time would not be entirely practical. I’m wrong?
That’s still not quite what I’m getting at, I’m saying you can cache their values and save it on exit. What I mean by this is storing values in a table when they’re updated, saving occasionally, and then saving the final cache value on exit.