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

Properly using pcall when using SetAsync via Datastore Service?

Asked by 4 years ago
Edited 4 years ago

So, I've been pcalling GetAsync and SetAsync but I'm not sure if I'm creating the right backup plan:

local DSS = game:GetService("DataStoreService")
local exampleDataStore = DSS:GetDataStore("Example1")

local function saveData(Player) -- pretend i got the player
    local suc, err = pcall(function()
        local emptyTable = {} -- pretend there is data here
        DSS:SetAsync(Player.UserId, emptyTable)
    end)

    if not suc then
        print(err)
        repeat wait()
            suc, err = pcall(function()
                local emptyTable = {} -- pretend there is data here
                DSS:SetAsync(Player.UserId, emptyTable)
            end)
        until suc do end
    end
end

so I'm wondering if i'm doing this correctly to secure the player data using pcall. Any tips or feedback on more efficient coding or better methods will be appreciated.

Edit: Some people have told me I'm doing it wrong

1 answer

Log in to vote
2
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

You're checking for the wrong success variable. you are in scope of the first suc variable meaning you are checking if the first success variable is true, which, if you error it will never be and you'll be stuck in an infinite loop forever. You instead want to be checking if the second success variable is true/false.

another thing that might end up looping forever is if we can't save the data. set a maximum amount of tries like in the example below so that doesn't become an issue.

Another thing, is you are asking the Datastore Service to use SetAsync() where you want to have the exampleDataStore use SetAync()

We also want to save the player's data, not an empty table, so I included a place to store all of the player's in game's data so you can access it when you want to save to datastore.

Not required, but you can use a module script so you can save / get the data from any script.

lastly, i'd suggest u using updateasync() over setasync() as setasync might not be the most recent data causing data loss.

PLEASE, PLEASE pLEaSE consider DOING THIS IF YOU DONT WANT TO LOSE DATA

i wrote about update async here

and i got the idea of using update async like that from here:

-- this is a table containing the in game data 
--you can access the player's data by saying AllPlayerData[playerNameGoesHere]
(in english, AllPlayerData AT playerNameGoesHere)

AllPlayerData = {
    [playerNameGoesHere] = {} 

}

local DSS = game:GetService("DataStoreService")
local exampleDataStore = DSS:GetDataStore("Example1")

local function saveData(player)
    local success = nil

    --the player has data, attempt to save that data
    success , err = pcall(function()
        exampleDataStore:SetAsync(player.UserId, AllPlayerData[player.Name])
    end)

    --setasync() failed, attempt three more times until we save successfully or go over our attempt limit
    if not success then
        local attempts = 0
        print(err)

        while(not success or attempts < 3)do
            wait(3)
            success, err = pcall(function()
                exampleDataStore:SetAsync(player.UserId,  AllPlayerData[player.Name])
                    end)
            attempts = attempts + 1
        end
    end
end
0
@royaltoe thanks, I just roughly wrote it since I'm in school so I want to check if it's not successful? Also why do we have a attempt I know it's not to spam requests but do we just let it be if it fails? BlackOrange3343 2676 — 4y
0
sure thing. we have an attempt because if for some reason, setasync() isn't working, we want to give up at some point. we don't want the loop to get stuck in an infinite loop trying to save the data when it can't. this might happen if roblox's setasync() isn't working (for whatever reason) or if the setasync() fails because our player variable is nil (aka we didn't pass in the player in our functi royaltoe 5144 — 4y
0
@royaltoe what would be a recommended amount of attempts? BlackOrange3343 2676 — 4y
0
@royaltoe also sorry to bother you but I do have an unsolved problem wondering if you could check it out: BlackOrange3343 2676 — 4y
View all comments (5 more)
0
if you want to do it that way, maybe 3-5 attempts with an attempt every 30 seconds-1 min? it's up to you really. i did something similar in my popular game and didn't have many issues with datastore. can't quite remember what i did and don't wanna bother going through my code, but that sounds fine to me. royaltoe 5144 — 4y
0
rn i just use UpdateAsync() and save the player's data every 5 mins + save when they leave the game/BindToClose() but should be able to do both the way you want to do it in your code and autosave every 5 mins if youd like royaltoe 5144 — 4y
0
@royaltoe thanks! Could you check out my other unsolved problem :3 BlackOrange3343 2676 — 4y
0
yea ill take a look and woah that is an ancient post royaltoe 5144 — 4y
Ad

Answer this question