Hi! I have a auto data saving script, with pcall function, but sometimes pcall doesn’t work, i don’t know how to fix that, can someone help me?
function PlayerData:DataSaving(player)
local key = tostring(player.UserId)
local success, errormessage = pcall(function()
DataStore:SetAsync(player.UserId, self.PlayersData[tostring(player.UserId)])
end)
if success then
print("Data successfully saved for "..player.Name.." and cleared from server")
table.remove(self.PlayersData, key)
elseif not success then
warn("Error x029y38")
local Retry_Counter = 1
while Retry_Counter >= 6 do
wait(20)
local successtry, errortry = pcall(function()
DataStore:SetAsync(player.UserId, self.PlayersData[tostring(player.UserId)])
end)
if successtry then print("Successfully saved data for "..player.Name.."and cleared from server in "..tostring(Retry_Counter).." tried")table.remove(self.PlayersData, key) break end
Retry_Counter = Retry_Counter + 1
end
else
warn("Can't save data")
end
end
it could be that it is waiting far too long between retries. This depends on how much data you are saving though. I’ve found that for just your average bit of data about 3 seconds is enough time.
with the pcall there is no real need for the third else as it either has success or it doesn’t. which means you only need if success and if not success.
it only has 6 attempts to save data and after that it just completely gives up, this is no good as players could lose their progress.
rather than having a second set you can do while success == nil, this means that it will keep going until it saves the data. If you are worried about having one request stopping others going though you can make a DSS que system where you have a table holding the data to save and to who. Then you can go through the table trying to save each one so one save error cant stop the entire system. (I just thought of this right now, going to add this to my own DSS functions soon)
thats the issue, you are trying to save a list.
You can easily convert the list into a single string and back out
You could use table.concat but as you have numbers we will loop it.
--- Turn into String
local StringData = ""
for i = 1,#Data do
StringData = StringData .. ";Split;" .. tostring(Data[i])
end
--- Turn Back into Table
local Data = string.Split(StringData,";Split;")
All this does is combine everything in the array with ;Split; Between each entry.
The ;Split; is the point where it is separated back out.
So for example:
“0;Split;0;Split;0;Split;1;Split;1” would be the start to the string of the given array
Then when converted back it will be a table again. (You will need to turn it back into numbers. But as it is a table you can loop through it)
ok, i understood you, but, idk why i can’t make cycle
local StringData = ""
for i = 1, #self.PlayersData[key] do
StringData = StringData..";Split;"..self.PlayersData[tostring(player.UserId)][i]
end
local Data = string.split(StringData, ";Split;")
print(unpack(Data)) --| - Server - PlayerDataModule:293 *nothing*
So how to get info from player data list?
I tried to use for i,v... but there is random cycle( for example 1,6,2,3)
so i made something like that, if it will work good?
local StringData = ""
for i,v in pairs(self.PlayersData[key]) do
StringData = StringData..";Split;"..tostring(v)
end
local Data = string.split(StringData,";Split;")
local EndData = {}
for i,k in pairs(Data) do
for v,b in pairs(self.PlayersData[key]) do
if tostring(k) == tostring(b) then
EndData[v] = b
end
end
end