so, I have a question, how to save the table correctly?
I know it works like this:
local Inventory = {50,87787,44,545,312,231,true,"test",4,false}
local succes, newdata = pcall(function()
playerdata3:SetAsync(playerId,Inventory)
end)
if succes then
print("Succes save coin data")
else
warn("Error save data")
end
and we get the data like this:
local PlayerInventory = playerdata:GetAsync(playeruserid)
if PlayerInventory then
for i, item in pairs(PlayerInventory) do
print(item)
warn("succes")
end
else
warn("error load inventory")
end
But the problem is that if the player joins and there is an error loading the data, after leaving the data could be overwritten to none? how to prevent it? You probably can’t use anything except :SetAsync(). Or not?
I usually use :IncrementAsync() or :UpdateAsync() for values that are not in the table, which prevents the data from being deleted.
You can prevent it by adding a debounce within the player and naming it a corrupted file. Change the boolean to true once you find out the data has been corrupted.
Getting the data:
local PlayerInventory = playerdata:GetAsync(playeruserid)
local deb = Instance.new("BoolValue", player); deb.Name == "Corrupt"
if PlayerInventory then
for i, item in pairs(PlayerInventory) do
print(item)
warn("succes")
end
else
warn("error load inventory")
deb.Value = true
end
Saving the data:
local Inventory = {50,87787,44,545,312,231,true,"test",4,false}
local succes, newdata = pcall(function()
if not player.Corrupt.Value then
playerdata3:SetAsync(playerId,Inventory)
end)
if succes then
print("Succes save coin data")
else
warn("Error save data")
end
Interesting idea, the only problem is that the error load inventory is every time the player joins the game for the first time, so this solution would make it impossible to save data for new players, but I’ll think about it.
Using pcalls when loading data allows you to easily make the game “spot” new players.
If the data is nil, then the game knows that the data didnt load, which usually means the user hasnt played the game before.
local Data
local Success, errorMessage = pcall(function()
Data = playerdata:GetAsync(playeruserid)
end)
if Success and Data then
print('Saved data Loaded')
elseif Success and not Data then
warn('New player, no saved data exists')
else
warn('Error Loading Data', errorMessage)
end