so, i make a leaderboard and every player that is about to leave its save all the values inside of the leaderboard (or its supposed to be)
however, when the players leaves, it save all the values in a table. and when its the time to save in datastore, it does not save
i tried to build a pcall function, it didn,t work
local LeaderstatsValues = {["Floors"] = 0, ["Coins"] = 0}
local Datastore = game.DataStoreService
game.Players.PlayerAdded:Connect(function(p)
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = p
local PlrDatastore = Datastore:GetDataStore("Leaderstats"):GetAsync(p.UserId)
for Name, Value in pairs(LeaderstatsValues) do
local Value2 = Instance.new("IntValue")
if PlrDatastore ~= nil then
if table.find(PlrDatastore, Name) then
Value2.Value = PlrDatastore[Name]
else
Value2.Value = Value
end
else
Value2.Value = Value
end
Value2.Name = Name
Value2.Parent = Leaderstats
end
end)
game.Players.PlayerRemoving:Connect(function(p)
local LeaderstatsSave = {}
local Leaderstats = p:FindFirstChild("leaderstats")
local PlrDatastore = Datastore:GetDataStore("Leaderstats")
if Leaderstats then
for i, Inst in pairs(Leaderstats:GetChildren()) do
LeaderstatsSave[Inst.Name] = Inst.Value
end
print(LeaderstatsSave)
PlrDatastore:SetAsync(p.UserId, LeaderstatsSave)
print(PlrDatastore:GetAsync(p.UserId))
end
end)
i typically say this on every single datastore post but add this to the end of your script and see if it works
game:BindToClose(function()
task.wait(3) --This gives the game time to save your data
end)
Add pcalls around your GetAsync and SetAsync, even if that isn’t the cause of your issue. If someones data fails to load/save, it will break the script for everyone else.
Make sure that you are saving an array. Datastores can’t save tables(i think).
Try this in the table part of the script
for i, Inst in ipairs(Leaderstats:GetChildren()) do
table.insert(Leaderstatssave, Inst.Value)
end
I’ve had this problem before as well. Sometimes studio glitches out and, when leaving, it doesn’t save information to the DataStore. My fix was that, every time I wanted to stop testing in studio, I used the escape menu and the leave button. But there might be a different issue.
You might as well want to use pcall() when getting data from or setting data to DataStores.