Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Datastore loses data often, even after all I did to prevent that?

Asked by 5 years ago

Gotta say first, I hate datastores, I'm not too bad at them, but they're really difficult and lose data often, this shouldn't be happening though, I even check if the getasynced data is equal to the setasynced data, if anyone has an idea or sees something bad in the code, please help.

local DataNum = script.Parent:WaitForChild('DataNum')
local DataStore = game:GetService("DataStoreService")

local PlayerSave = DataStore:GetDataStore("PS_"..DataNum.Value)
local SaveGUI = script.Parent:WaitForChild('SaveGUI')

-- or {0,0,2,1,20, 100, 240, 0, 1, 1, 1, 1000, 1, 1, 1, 1, 1, 1, 100000, 777, 10000, 10, 1}

local function writeplr(plr)

end

local function load(plr)
    local key = 'Key_'..plr.UserId
    local Stats = plr:WaitForChild('Stats')

    local UpGradeFrame = plr:WaitForChild("PlayerGui"):WaitForChild("Main"):WaitForChild("UpgradeFrame")

    local success, message = pcall(function()

        local success, saves = pcall(function()
            local saves = PlayerSave:GetAsync(key)
            return saves
        end)

        if saves then

            Stats:WaitForChild('Cash').Value = saves[1] or 0
            Stats:WaitForChild('CashL').Value = saves[2] or 0
            Stats:WaitForChild('Damage').Value = saves[3] or 2
            Stats:WaitForChild('BuxDrop').Value = saves[4] or 1
            Stats:WaitForChild('TixDrop').Value = saves[5] or 20
            Stats:WaitForChild('BagDrop').Value= saves[6] or 100
            Stats:WaitForChild('AutoClick').Value = saves[7] or 240
            Stats:WaitForChild('NetWorth').Value = saves[8] or 0
            Stats:WaitForChild('Multiplier').Value = saves[9] or 1
            Stats:WaitForChild('UltraR').Value = saves[10] or 1
            Stats:WaitForChild('Square').Value = saves[11] or 1
            plr.Character:WaitForChild('Humanoid').MaxHealth = saves[12] or 1000

            UpGradeFrame:WaitForChild('ACUpgrade').Button.Times.Value = saves[13] or 1
            UpGradeFrame.BagUpgrade.Button.Times.Value = saves[14] or 1
            UpGradeFrame.BuxUpgrade.Button.Times.Value = saves[15] or 1
            UpGradeFrame.HPUpgrade.Button.Times.Value = saves[16] or 1
            UpGradeFrame.TixUpgrade.Button.Times.Value = saves[17] or 1
            plr.PlayerGui.Main.SquareFrame.Chance.Value = saves[18] or 777
            plr.PlayerGui.Main.SquareFrame.SDO.Value = saves[19] or 10000
            UpGradeFrame.DMGUpgrade.Button.Times.Value = saves[20] or 1

            local Loaded = Instance.new("BoolValue")
            Loaded.Value = true
            Loaded.Name = "Loaded"
            Loaded.Parent = plr

        else
            if plr:FindFirstChild('LoadAttempts') then
                local Attempts = plr:FindFirstChild('LoadAttempts')
                if Attempts.Value > 3 then
                    local Loaded = Instance.new("BoolValue")
                    Loaded.Value = true
                    Loaded.Name = "Loaded"
                    Loaded.Parent = plr
                else
                    wait(5)
                    Attempts.Value = Attempts.Value + 1
                    load(plr)
                end
            else
                local Attempts = Instance.new('IntValue')
                Attempts.Name = 'LoadAttempts'
                Attempts.Value = 1
                Attempts.Parent = plr
            end
        end
    end)
end

game.Players.PlayerAdded:Connect(function(plr)
    load(plr)
end)

function SavePlr(plr)

    local SavingMessage = SaveGUI:Clone()
    SavingMessage.Name = 'SavingMessage'

    local mainText = SavingMessage:WaitForChild('mainText')
    local mScript = mainText:WaitForChild('Main')

    local success, message = pcall(function()

        local key = 'Key_'..plr.UserId
        local Stats = plr:WaitForChild('Stats')

        local UpGradeFrame = plr:WaitForChild("PlayerGui"):WaitForChild("Main"):WaitForChild("UpgradeFrame")

        local ValuesToSave = {

            Stats:WaitForChild('Cash').Value,
            Stats:WaitForChild('CashL').Value,
            Stats:WaitForChild('Damage').Value,
            Stats:WaitForChild('BuxDrop').Value,
            Stats:WaitForChild('TixDrop').Value,
            Stats:WaitForChild('BagDrop').Value,
            Stats:WaitForChild('AutoClick').Value,
            Stats:WaitForChild('NetWorth').Value,
            Stats:WaitForChild('Multiplier').Value,
            Stats:WaitForChild('UltraR').Value,
            Stats:WaitForChild('Square').Value,
            plr.Character:WaitForChild('Humanoid').MaxHealth,

            UpGradeFrame.ACUpgrade.Button.Times.Value,
            UpGradeFrame.ACUpgrade.Button.Times.Value,
            UpGradeFrame.BagUpgrade.Button.Times.Value, 
            UpGradeFrame.BuxUpgrade.Button.Times.Value,
            UpGradeFrame.HPUpgrade.Button.Times.Value,
            UpGradeFrame.TixUpgrade.Button.Times.Value,
            plr.PlayerGui.Main.SquareFrame.Chance.Value,
            plr.PlayerGui.Main.SquareFrame.SDO.Value,
            UpGradeFrame.DMGUpgrade.Button.Times.Value
        }

        if ValuesToSave and plr and plr:FindFirstChild('Loaded') then
            PlayerSave:SetAsync(key, ValuesToSave)

            local TestValues = PlayerSave:GetAsync(key)
            if TestValues == ValuesToSave then
                mainText.Text = 'Saved!'

                mScript.Disabled = false
                SavingMessage.Parent = plr:WaitForChild('PlayerGui')
            else
                mainText.Text = 'Error saving values! (datastore does not match to stats)'
                mainText.TextColor3 = Color3.fromRGB(255,0,0)

                mScript.Disabled = false
                SavingMessage.Parent = plr:WaitForChild('PlayerGui')
            end
        end
    end)

    if success then
        mainText.Text = 'Saved!'

        mScript.Disabled = false
        SavingMessage.Parent = plr:WaitForChild('PlayerGui')
    else
        mainText.Text = 'Failed to save: '..message
        mainText.TextColor3 = Color3.fromRGB(255,0,0)

        mScript.Disabled = false
        SavingMessage.Parent = plr:WaitForChild('PlayerGui')
    end
end

while wait() do
    for i, plr in pairs(game.Players:GetPlayers()) do
        SavePlr(plr)
        wait((60/#game.Players:GetPlayers())/2)
    end
end

If you're confused here wait((60/#game.Players:GetPlayers())/2), what is does basicly is waiting 30 seconds/save. For errors, no I don't get them, for throtthles or so, not sure, It's in team create so I can't see the dev console.

0
oof DeceptiveCaster 3761 — 5y
0
Just noticed I should add 'and success' at line 026, don't think that's the main problem though User#20388 0 — 5y
0
wdym lol User#20388 0 — 5y
0
I'm a bit affraid that nobody can help, datastores are so damn difficult rip User#20388 0 — 5y
View all comments (4 more)
0
maybe somebody can, but im not sure DeceptiveCaster 3761 — 5y
0
Try to find anything bad in my code though, this needs to be fixed rip User#20388 0 — 5y
0
:eyes: vanilla_wizard 336 — 5y
0
That's a pointless unhelpfull comment ^^ User#20388 0 — 5y

Answer this question