My datastore keeps randomly resetting, idk if its a loading issue or what but any help?
game.Players.PlayerAdded:Connect(function(plr)
local stats = Instance.new("Folder", plr)
stats.Name = "stats"
local Data = nil
pcall(function()
Data = ds:GetAsync(Prefix)
end)
if Data ~= nil then
for i,v in pairs(stats:GetChildren()) do
v.Value = Data[v.Name]
end
else
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local Prefix = plr.UserId
local leaderstats = plr.stats
pcall(function()
if leaderstats then
local SaveData = {}
for i,v in pairs(leaderstats:GetChildren()) do
SaveData[v.Name] = v.Value
end
ds:SetAsync(Prefix, SaveData)
end
end)
end)
Do you have any form of auto save / manual save? If this is all you have for data saving it may reset just because of lack of saving and or some sort of data loss.
game.Players.PlayerAdded:Connect(function(plr)
local stats = Instance.new("Folder", plr)
stats.Name = "stats"
local Data = nil
pcall(function()
Data = ds:GetAsync(Prefix)
end)
if Data ~= nil then
for i,v in pairs(stats:GetChildren()) do
v.Value = Data[v.Name]
end
else
end
end)
Try printing the data once you load it to find out if it’s loading.
This script was working just fine for me.
local ds = game:GetService("DataStoreService"):GetDataStore("newdatastore1")
game.Players.PlayerAdded:Connect(function(plr)
local stats = Instance.new("Folder", plr)
stats.Name = "stats"
local Prefix = plr.UserId
local Data = nil
pcall(function()
Data = ds:GetAsync(Prefix)
end)
if Data ~= nil then
for i,v in pairs(stats:GetChildren()) do
v.Value = Data[v.Name]
end
else
print("Is nil")
end
print(Data)
end)
game.Players.PlayerRemoving:Connect(function(plr)
local Prefix = plr.UserId
local leaderstats = plr.stats
pcall(function()
if leaderstats then
local SaveData = {}
for i,v in pairs(leaderstats:GetChildren()) do
SaveData[v.Name] = v.Value
end
print(SaveData)
ds:SetAsync(Prefix, SaveData)
end
end)
end)
your pcall currently doesn’t do much except, basically ignoring errors, and if it errors, the data won’t get saved.
try letting the pcall return if the save was successful or not, example:
game.Players.PlayerRemoving:Connect(function(plr)
local Prefix = plr.UserId
local leaderstats = plr:FindFirstChild("stats")
if leaderstats then
local function saveData()
local success, fail = pcall(function()
local SaveData = {}
for i,v in pairs(leaderstats:GetChildren()) do
SaveData[v.Name] = v.Value
end
ds:SetAsync(Prefix, SaveData)
end)
if not success then -- retry if failed
wait()
saveData()
end
end
saveData()
end
end)
I’d also suggest having the pcall return success or fail when you are loading data
Your problem is you don’t have a game:BindToClose function, that fires when the server shuts down. This results in your data not saving so it gets reset. Just loop through all the players in the bindToClose function, and save their data.
Another issue with your code is, that datastores are web requests, meaning they can randomly fail from time to time. If the pcall does not return success, keep trying until it does, alternatively you could have a maxAttempts.