Hello, Scripting Support. I once again request your help. I am trying to figure out the problem with my Datastore. I have not found a solution on the forums, so I decided to ask directly.
local sessiondata = {
}
local function savedata(player)
if sessiondata[player.UserId] then
local success = nil
local errorMsg = nil
local attempts = 1
repeat
success,errorMsg = pcall(function()
datastore:SetAsync(player.UserId.."-Data", sessiondata[player.UserId])
end)
attempts += 1
if not success then
warn(errorMsg)
task.wait(3)
end
until success or attempts == 5
if success then
print(player.Name .. ", Data Saved")
else
warn(player.Name .. ", Issue Saving Data")
end
end
end
local function loaddata(player)
local attempts = 0
local success = nil
local playerdata = nil
repeat
success, playerdata = pcall(function()
return datastore:GetAsync(player.UserId.."-Data")
end)
attempts += 1
if not success then
warn(playerdata)
task.wait(3)
end
until attempts == 5 or success
if success then
print(player.Name .. ", data successfully loaded!")
if not playerdata then
playerdata = {
["Time"] = 0
}
end
sessiondata[player.UserId] = playerdata
else
player:Kick("Data could not be loaded. Try again later!")
end
end
players.PlayerAdded:Connect(function(player)
loaddata(player)
local data = sessiondata[player.UserId]
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local playertime = Instance.new("IntValue")
playertime.Name = "Time"
playertime.Parent = leaderstats
playertime.Value = data["Time"]
playertime.Changed:Connect(function()
data["Time"] = playertime.Value
end)
while task.wait(1) do
playertime.Value += 1
end
end)
game:BindToClose(function()
for i,player in pairs(players:GetPlayers()) do
task.spawn(function()
savedata(player)
end)
end
end)
players.PlayerRemoving:Connect(function(player)
savedata(player)
end)
This is all the related code. But the main problem is Saving. The game only seems to save data around half the time. I can’t for the life of me figure out what the problem might be. Help would be appreciated. Many thanks.
If nothing is printed in the output that means there probably isn’t a sessiondata for that player. I think this is because when u press Play in studio your player will also be the server so it doesn’t fire the PlayerAdded because your player joined before that event was run. Try using Team Test because that will open a separate server before your player joins.
Could you try adding some more wait to the BindToClose function?
If it really saves 50% of the time, it might be related to that.
game:BindToClose(function()
for i,player in pairs(players:GetPlayers()) do
task.spawn(function()
savedata(player)
end)
end
if RunService:IsStudio() then
wait(3)
end
end)
I’m not sure if you still need help with this since i’m a little late, but if you do, are there any warnings notifying you of ratelimits in your Developer Console (F9)?
I’ve had issues with data saving/loading inconsistently in Studio. I believe this is caused by the server shutting down too quickly. If you publish the game and try to save data through the game client, it should save and load properly.
local DataStoreService = game:GetService("DataStoreService")
local datastore = DataStoreService:GetDataStore("PlayerDataStore")
local sessiondata = {}
local function savedata(player)
if sessiondata[player.UserId] then
local success = nil
local errorMsg = nil
local attempts = 1
repeat
success, errorMsg = pcall(function()
datastore:SetAsync(player.UserId.."-Data", sessiondata[player.UserId])
end)
attempts += 1
if not success then
warn(errorMsg)
wait(3)
end
until success or attempts == 5
if success then
print(player.Name .. ", Data Saved")
else
warn(player.Name .. ", Issue Saving Data")
end
end
end
local function loaddata(player)
local attempts = 0
local success = nil
local playerdata = nil
repeat
success, playerdata = pcall(function()
return datastore:GetAsync(player.UserId.."-Data")
end)
attempts += 1
if not success then
warn(playerdata)
wait(3)
end
until attempts == 5 or success
if success then
print(player.Name .. ", data successfully loaded!")
if not playerdata then
playerdata = {
["Time"] = 0
}
end
sessiondata[player.UserId] = playerdata
else
player:Kick("Data could not be loaded. Try again later!")
end
end
game.Players.PlayerAdded:Connect(function(player)
loaddata(player)
local data = sessiondata[player.UserId]
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local playertime = Instance.new("IntValue")
playertime.Name = "Time"
playertime.Parent = leaderstats
playertime.Value = data["Time"]
playertime.Changed:Connect(function()
data["Time"] = playertime.Value
end)
spawn(function()
while wait(1) do
playertime.Value += 1
end
end)
end)
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
task.spawn(function()
savedata(player)
end)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
savedata(player)
end)