Problem with function return

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.

Where exactly should I return success? Before or after until?

You also have a return success after the until. Just change it to return true.

until tries >= MAX_SAVE_TRIES or success

   return true

Hey there!

I think the issue with your code is coming from your return statement inside of the pcall function. You’re returning what you’re getting.

2 Likes

Yeah I noticed that the problem is in pcall, but how do I fix it?

I already have return success after until tries >= MAX_SAVE_TRIES or success.

You’re trying to get information out of the data store, correct?

EDIT: My bad, you’re saving data.

I know you have that but you’re setting success to sessionData[playerUserId]. So return success just returns sessionData[playerUserId].

I’d just switch the return success with

return success ~= false and true or false

(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.

2 Likes

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.

Clearly, success becomes sessionData[playerUserId] because its pcall does return a value.

1 Like

The script works now after I implemented @b_dnd solution. I might still need to test some more to be sure.

1 Like