I am working on a points system and I followed a guide from another topic and still its not saving.
Here is the code for it.
local pointsData = game:GetService("DataStoreService"):GetDataStore("Points")
local backupData = game:GetService("DataStoreService"):GetDataStore("Backup")
game.Players.PlayerAdded:Connect(function(plr)
local Stored = Instance.new("Folder", plr)
Stored.Name = "leaderstats"
local rank = Instance.new("StringValue", Stored)
rank.Name = "Rank"
rank.Value = plr:GetRoleInGroup(4596247)
local points = Instance.new("NumberValue", Stored)
points.Name = "Points"
points.Value = pointsData:GetAsync(plr.UserId) or 0
print(pointsData:GetAsync(plr.UserId))
end)
game.Players.PlayerRemoving:Connect(function(plr)
local leaderstats = plr:FindFirstChild('leaderstats')
if leaderstats then
local saveData = {
leaderstats['Points'].Value
}
local Success, Error = pcall(function() -- Added pcall
pointsData:UpdateAsync(plr.UserId, function(OldData) -- Calls the UpdateAsync() function
return saveData
end)
end)
if not Success then
warn('Failed to save pointData for')
end
end
end)
game:BindToClose(function()
if not game:GetService("RunService"):IsStudio() then
return
end
local player = game:GetService("Players")
local players = player:GetPlayers()
for _, player in pairs(players) do
local userId = player.UserId
local points = pointsData:GetAsync(player.userId)
if points then
local success, result = pcall(function()
backupData:SetAsync(player.userId, points)
end)
if not success then
warn(result)
end
end
end
end)
I would highly suggest using DataStore2, it’s not just more reliable, but for me even easier to use! Check this video by AlvinBlox: https://www.youtube.com/watch?v=hBfMfB0BwGA
BindToClose makes sure that the datastore saves no matter where or what you test this on by stalling the server before shutdown. Also, studio access to api needs to be enabled of course.
Did you even read what I said? On studio, updating is highly RANDOM. You know it’s updating if the game is taking longer than usual to close. If it closes fast, then it’s not saving…
Have you tried a simplified version and got that working? Just something simple such as saving total playtime where you just retrieve data and set data.
This is a datastore handling script which I wrote a good while ago. It might be helpful to you if you take a look at it and compare my code to your code to see if you’re missing something.
For some reason I decided back then that it was a good idea to save the data every interval. Though you can easily change this to only happen on player removal and server close.
Edit: Here's an updated version which saves on BindToClose as well.
-- DATASTORE HANDLING
local _version = 0 -- Changing this will reset the datastore
local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetDataStore("TimePlayed" .. _version)
function getData(player)
local success, data = pcall(function()
return datastore:GetAsync(player.UserId)
end)
if success then
return data
else
warn("error while trying to retrieve " .. player.Name .. "'s data")
end
end
function saveData(player, _data)
local success = pcall(function()
return datastore:SetAsync(player.UserId, _data)
end)
if not success then
warn("error while trying to save " .. player.Name .. "'s data")
end
end
game.Players.PlayerAdded:Connect(function(player)
local _data = getData(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local timeVal = Instance.new("NumberValue")
timeVal.Name = "Time Played"
timeVal.Value = _data or 0
timeVal.Parent = leaderstats
end)
game:BindToClose(function()
for i, v in pairs(game.Players:GetChildren()) do
local leaderstats = v:FindFirstChild("leaderstats")
if leaderstats then
saveData(v, leaderstats["Time Played"].Value)
end
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local leaderstats = Player:FindFirstChild("leaderstats")
if leaderstats then
saveData(Player, leaderstats["Time Played"].Value)
end
end)
spawn(function()
while wait(1) do
for i, v in pairs(game.Players:GetChildren()) do
local leaderstats = v:FindFirstChild("leaderstats")
if leaderstats then
leaderstats["Time Played"].Value = leaderstats["Time Played"].Value + 1
end
end
end
end)