My save player data function does not return the right value. Here is the code:
function playerDataModule.savePlayerData(player, data)
local playerUserId = "Player_"..player.UserId
-- Check if session data exists or data is not nil.
if sessionData[playerUserId] then
local tries = 0 -- Count how many tries for saving.
local success, errorMessage = false, ""
-- Try a few times to save data.
repeat
tries = tries + 1
-- Try to update or set player data.
success, errorMessage = pcall(function ()
local hasSavedData = playerData:GetAsync(playerUserId)
-- Check if data already exists in data store.
if hasSavedData and data == nil then
playerData:UpdateAsync(playerUserId, function(old)
return sessionData[playerUserId] -- this should return to variable success, but it returns the whole function.
end)
else
playerData:SetAsync(playerUserId, data or sessionData[playerUserId])
end
end)
until tries >= MAX_SAVE_TRIES or success
return success
end
return false
end
Instead of returning success, the function returns sessionData[playerUserId].
On your until where you check if success exist just return true if you don’t want to to return the sessionData. It is already checking if success exist so no reason to have to return sucesss again.
(that is using a ternary statement)
pretty much means if success is not false (which also counts for things other than just booleans), then return true. otherwise, return false.
savePlayerData() should return true if saving data was a success, or false if could not save data.
playerData:UpdateAsync(playerUserId, function(old)
return sessionData[playerUserId] -- this should return to variable success, but it returns the whole function.
end)
for some reason when I use UpdateAsync, return does not return to pcall, but actually the whole function. Sorry for not explaining it too well, I am a bit confused myself.