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

How do I error-proof this datastore saving system?

Asked by
trecept 367 Moderation Voter
6 years ago
SaveData = function(plr)
    local lstats = plr:FindFirstChild("leaderstats")
    if lstats~=nil then
        local Level = lstats:FindFirstChild("Level")
        local EXP = plr:FindFirstChild("EXP")
        local Coins = lstats:FindFirstChild("Coins")
        if Level~=nil and EXP~=nil and Coins~=nil then
            local tablestats = {
                Level.Value,
                EXP.Value,
                Coins.Value
            }
            local GetData = PlayerStats:GetAsync(plr.UserId)
            if GetData then
                PlayerStats:UpdateAsync(plr.UserId, function() return tablestats end)
            else
                PlayerStats:SetAsync(plr.UserId,tablestats)
            end
        end
    end
end

LoadData = function(plr,Level,EXP,Coins)
    local GetData = PlayerStats:GetAsync(plr.UserId)
    if GetData then
        Level.Value = GetData[1]
        EXP.Value = GetData[2]
        Coins.Value = GetData[3]
    else
        Level.Value = 1
        EXP.Value = 0
        Coins.Value = 0
    end
end

This is my current script for saving stats of players (Levels, EXP and Coins) it works perfectly fine so far and I'm happy with it, but I know and have been told datastore errors occur and I should prepare for them. How would I make these datastore functions be able to handle errors that may occur so I could possibly kick the player if their data could be removed or not saved?

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Use pcalls

The wiki also has a great example/tutorial of error handling for Data Stores here.

Ad

Answer this question