Pcall(function()) not returning it's success or error?

Hello, I’m re-writing my DataStore to save everything in one table/dictionary, when I get to saving the data (PlayerRemoving) and run the pcall, nothing happens. It doesn’t print the success or error message.
The actual table/dictionary is as follows →

Data = {crystals=(IntValue),gems=(IntValue),doors=(table of bool values),items=(table of int values)}

When I try and run the pcall:

local Datasuccess, DataErrorMsg = pcall(function()

Nothing happens, in the if statement to check weather it returns as an error or success, nothing happens as well. If I put a print() statement above the SetAsync() it prints, but below it doesn’t print. Leading me to believe the SetAsync() isn’t firing and I’m not sure why.
The table is correct as well, if I print each part of the table/dictionary it returns correctly, I’m not sure what I might be missing so any help is appreciated. Thanks in advanced,

No need to sign your posts, your username is already on your posts.


I don’t know what you mean?

:SetAsync was called, you probably didn’t print the information given correctly.

-- I used this code and it works.

local successful, exception_data = pcall(DataSave.SetAsync, DataSave, DataKey, Data)
print(successful and "Yes" or "No", exception_data or "NO FAIL :D")

Also Stop using SetAsync() to save player data


I have correctly set the print statement to print either a yes or no if it saved. I also tried your method above using the pcall() and still no print or error was given.
However, when I load the game Data is presented as “nil” and I get the following error message.
attempt to index local ‘Data’ (a nil value)
This error is given when I join the game within a PlayerAdded function, and have looked into this error as well with no success as to fix it.

Could you copy and paste the relevant code in the PlayerAdded listener? As well as relevant code in the PlayerRemoving listener?

Sure thing

-- Data
local Data -- This is for the Int Values
local DoorData -- Data Table for the table of bool values
local ItemData -- Data Table for the table of int values
local DataSuccess, DataErrorMsg = pcall(function()
	Data = DataSave:GetAsync(DataKey)
if DataSuccess then
	cry.Value = Data.crystals -- where the error originated from
	gms.Value = Data.gems
	for i, v in pairs(drs:GetChildren()) do
		local DataTable = Data.doors
		v.Value = DataTable[i]
	for a, b in pairs(itms:GetChildren()) do
		local DataTable = Data.items
		b.Value = DataTable[a]

Now this probably isn’t all that great code, so any tips to improve it would be nice as well as I’m still learning same with the saving section.

local function GetDoorValues()
	local DoorValues = {}
	for _, v in pairs(d_folder:GetChildren()) do
	return DoorValues

local function GetItemValues()
	local ItemValues = {}
	for _, v in pairs(i_folder:GetChildren()) do
	return ItemValues
local d = GetDoorValues() -- works
local i = GetItemValues() -- works
local Data = {crystals=c.Value,gems=g.Value,doors=d,items=i} -- table/dictionary is correct printing each section correctly
-- Data
local Datasuccess, DataErrorMsg = pcall(DataSave.SetAsync,DataSave,DataKey,Data)
print(Datasuccess) -- this doesn't print
print(DataErrorMsg)-- this doesn't print either

Also I tried different methods of the pcall as well. Like the first one in the original post but that didn’t work either.

pcall is meant to capture errors, consider the following.

function doSomething() 
if pcall(doSomething) then
	--No errors
	print("error") --A isnt a valid table

a might not be a table, therefor, it would print “error” because A is not a valid table
See Programming in Lua : 8.4 for more info

This is a terrible way to use pcall. Don’t use the call as the condition of an if statement, use what it returns as the condition of an if statement. You’re effectively throwing away the values by using pcall as the condition of an if statement which defeats the purpose of it being an error handler.

local function doSomething()

local success, ret = pcall(doSomething)

if success then
    -- Succeeded
    if ret then
        -- We have returned data
        -- We do not have returned data
    -- Not successful