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!
5 comments
I tried answering, but you're right. The wiki can be frustrating.
Its a module function , which can be called from any script.
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. "
That was written above the line of codes you borrowed . Please read paragraphs at times , even if its a pain...
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