So I had this code which would RemoveAsync your data because I thought it would just wipe your data and when you joined back it would give you default data. However, people who got wiped don’t have anything like at all not even default data, they just can’t have data and whenever they join it’s nil instead of loading in default data. This is my code, how can I change it to like make it so they can actually have data again:
local function loadData(player)
print(player)
local success = nil
local playerData = nil
local attempt = 1
repeat
success, playerData = pcall(function()
return dataBase:GetAsync(player.UserId)
end)
attempt += 1
if not success then
warn(playerData)
task.wait()
end
until success or attempt == 3
if success then
if not playerData then--give default data if they're new
playerData = {
["Gems"] = 0,
["SelectedTowers"] = {"Cameraguy"},
["OwnedTowers"] = {"Cameraguy"},
["MaxTowers"] = 5,
["RedeemedCodes"] = {}
}
end
data[player.UserId] = playerData
That was kinda worded weirdly, basically, someones data got RemoveAsync’d and now their data is just permanently nil no matter how many times they rejoin, it won’t even give them default data, it’s just nothing
--save player data
local function saveData(player)
print(player)
if data[player.UserId] and player.UserId ~= 0 then
local success = nil
local playerData = nil
local attempt = 1
repeat
success, playerData = pcall(function()
return dataBase:UpdateAsync(player.UserId, function()
return data[player.UserId]
end)
end)
attempt += 1
if not success then
warn(playerData)
task.wait()
end
until success or attempt == 3
if success then
print("Data saved successfully")
else
warn("Unable to save data for player", player.UserId)
end
else
warn("No session data for", player.UserId)
end
end
Players.PlayerRemoving:Connect(function(player)
saveData(player)
data[player.UserId] = nil
end)
game:BindToClose(function()--if game shuts down
if not RunService:IsStudio() then
print("Shutting down")
for index, player in pairs(Players:GetPlayers()) do
task.spawn(function()
saveData(player)
end)
end
else
print("Shutting down inside studio")
end
end)
From what I’ve heard, Studio would fail to save sometimes when the test instance closes earlier than PlayerRemoving can finish executing, so data wouldn’t save.