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

The roblox wiki isn't explaining something, can someone please help or point out something?

Asked by 6 years ago

I'm trying to make my data saves safer so I went the wiki specifically this page: http://wiki.roblox.com/index.php?title=Saving_Player_Data

at the end, it mentions this

local function dataStoreRetry(dataStoreFunction)
    local tries = 0 
    local success = true
    local data = nil
    repeat
        tries = tries + 1
        success = pcall(function() data = dataStoreFunction() end)
        if not success then wait(1) end
    until tries == DATASTORE_RETRIES or success
    if not success then
        error('Could not access DataStore! Warn players that their data might not get saved!')
    end
    return success, data
end

do you see the dataStoreFunction() part well if it's a function then how come it's not in the rest of the tutorial if it's not a function then what does it do? Because they didn't mention this in any previous text or in a script part, I'm really frustrated. If the wiki can't say anything then what do I do if it's a success?

0
I tried answering, but you're right. The wiki can be frustrating. SebbyTheGODKid 198 — 6y
0
Its a module function , which can be called from any script. arshad145 392 — 6y
0
All it does is check for errors..."Since we want to retry the function several times, we can put this code inside of a loop that finishes only when the call succeeds or it has tried too many times. When we define this function, we can also have one of the parameters be the function we want to retry. This way this code can be used for both setting and getting values from the DataStore. " arshad145 392 — 6y
0
That was written above the line of codes you *borrowed* . Please read paragraphs at times , even if its a pain... arshad145 392 — 6y
0
dataStoreFunction is an argument and you need to pass a function to that argument which should return player data with GetAsync. What's so complicated about it? Maybe read up on functions and passing data to parameters Impacthills 223 — 6y

1 answer

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

If you look closer, dataStoreFunction actually is defined within the scope of the function. It's a parameter passed to the function:

local function dataStoreRetry(dataStoreFunction)

dataStoreRetry is called within two wrapper functions defined at the bottom of the script:

...

local function getPlayerData(player)
    return dataStoreRetry(function()
        return playerData:GetAsync(player.UserId)
    end)
end

local function savePlayerData(player)
    return dataStoreRetry(function()
        return playerData:SetAsync(player.UserId, sessionData[player])
    end)
end

Notice that when dataStoreRetry is called, anonymous functions are being passed in as the arguments. This means that when dataStoreRetry (the code in your question) runs, dataStoreFunction is equal to whichever anonymous function was passed in.

For example, let's say that we called getPlayerData(player), which then calls dataStoreRetry with the this as the dataStoreFunction argument:

function()
    return playerData:GetAsync(player.UserId)
end

You could substitute that function in for dataStoreFunction, so your code above would look like this:

local function dataStoreRetry()
    local tries = 0 
    local success = true
    local data = nil
    local function dataStoreFunction()
        return playerData:GetAsync(player.UserId)
    end
    repeat
        tries = tries + 1
        success = pcall(function() data = dataStoreFunction() end)
        if not success then wait(1) end
    until tries == DATASTORE_RETRIES or success
    if not success then
        error('Could not access DataStore! Warn players that their data might not get saved!')
    end
    return success, data
end

The reason for doing this is so they can use the same dataStoreRetry function to retry two different actions: GetAsync and SetAsync. All without having to rewrite the retry code for each action!

I know it's a lot, and requires a solid understanding of how functions can be used in Lua, but this is my best answer!

Ad

Answer this question