How can I avoid data loss (without using DataStorev2 or ProfileService) in the DataStore?

Hello, apparently I have been using the DataStore for 1 week and I am not very good with it but now that I tried to save the data with 7 people, it turned out that the data of those 7 people only 2 were not saved and I checked the code over and over again but I could not find any error and I do not know what to do with this code. Could you help me? Is something wrong with the code? Could you please help me.

Here is the code

local data = game:GetService("DataStoreService")
local Store = data:GetDataStore("DataStore_Service_code_" .. math.pow(5, 4))
CONFIG_DATS = {
    NameFolder = "leaderstats",
    NameCurrency = "Bores",
    KeyDataStore = "PLR_DATA_PLAYERID__"
}

local function GetDataStore_Data(Player)
    if game.Players:FindFirstChild(Player.Name) then
        local FolderStats = Instance.new("Folder", Player)
        FolderStats.Name = CONFIG_DATS.NameFolder
        local Point = Instance.new("NumberValue", Folder)
        Point.Name = CONFIG_DATS.NameCurrency
        local data = nil
        repeat wait()
            local Bool, String = pcall(function()
                data = Store:GetAsync(CONFIG_DATS.KeyDataStore .. Player.UserId) or math.random(1, 2)
            end)
            if Bool then Point.Value = data elseif not Bool then warn("\nError could not get the data correctly!") end
            if String then return end
        until moneda.Value > 0
    else
        warn("\nThe user does not exist!")
    end
end

local function SaveData(Player)
    if game.Players:FindFirstChild(Player.Name)then
        local FolderPro = jugador:FindFirstChild(CONFIG_DATS.NameFolder)
        if FolderPro then
            local CurrencyExist = FolderPro:FindFirstChild(CONFIG_DATS.NameCurrency)
            if CurrencyExist then
                local Value = CurrencyExist.Value
                if Value == 0 or CurrencyExist.Value == 0 then return end
                if Value >= 1 or CurrencyExist.Value >= 1 then
                    local OldData = Store:GetAsync(CONFIG_DATS.KeyDataStore .. Player.UserId)
                    if OldData == Value OldData == CurrencyExist.Value  then
                        print("\nThe same data cannot be saved over and over again.")
                    else
                        Store:SetAsync(CONFIG_DATS.KeyDataStore .. Player.UserId, CurrencyExist.Value)
                        print("\nLa data del usuario se guardo correctamente!")
                    end
                end
            end
        else
            warn("\nThe folder that holds the 'Values' of the player's data does not exist.")
        end
    else
        warn("\nThe user does not exist!")
    end
end

------>>// Calling the events and executing the functions. \\<<------
game.Players.PlayerAdded:Connect(GetDataStore_Data)
game.Players.PlayerRemoving:Connect(SaveData)
------>>// Saving the data a second time if it is not saved the first time. \\<<------
game:BindToClose(function()
    for i, Player in pairs(game.Players:GetChildren()) do
        SaveData(Player)
    end
end)
1 Like

Please read the following article:

Basically when the server closes they still store the player’s data.

1 Like

wow thank you, I had no idea about this!!! The coroutine is very awesome!!! Thank you so much for answering me and helping me

1 Like