Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does the wiki set pcall to two different variables in this case?

local function savePlayerData(playerUserId)
	if sessionData[playerUserId] then
		local success, err = pcall(function()
			playerData:SetAsync(playerUserId, sessionData[playerUserId])
		end)
		if not success then
			warn("Cannot save data for player!")
		end
	end
end

My understanding is that pcall() returns whether the code was run without error or not as its first value, which I understand. Why does the wiki also set pcall() to err, though? Is it just a mistake?

1 answer

✓ Accepted answer

Check the manual! pcall returns multiple values.

Its first result is the status code (a boolean), which is true if the call succeeds without errors. In such case, pcall also returns all results from the call, after this first result. In case of any error, pcall returns false plus the error message.

The example on the Wiki is bad form, though, since they're defining err without actually using it. They should probably have included it in the warning message, so that debugging why it's not saving is easier:

if not success then
	warn("Cannot save data for player: " .. err)
end

This discussion is preserved as it was. New answers, comments, and votes are closed.