GetAsync not erroring

Hi! I have a pcall set up for a data store and when I dont have data in a specific key the pcall returns successful but the datastore values comes up nil? Shouldn’t the pcall return failed if the datastore returns nil?

local success, datastoreval = pcall(function()	
		  levelsUnlockedDataStore:GetAsync("Level0Unlocked"..playerID)
		if datastoreval == nil then
			error()
		end
	end)

	if success == false then -- success returns true even when datastoreval is nil
		warn("GetAsyncFailed! Creating new data.")
		levelsUnlockedDataStore:SetAsync("Level0Unlocked"..playerID, true)
	else 
		warn("Data found! Data Retrieved:",datastoreval)
	end

I had to add the error() function to get it to work properly, I also use a plugin to remove the data so there shouldnt be any data.

Put the if statement after the pcall()
for the error part in the if statement, you can use assert in this sense:

    local success, datastoreval = pcall(function()	
		 return levelsUnlockedDataStore:GetAsync("Level0Unlocked"..playerID)
    end)

    assert(datastoreval, "No Data") -- fails if item is nil or false
    -- Param 1: Value
    -- Param 2: Error Message

for the if statement

if not datastoreval then -- if data is nil
    -- code
end

You are also placing code after return, never do this, because the code will not fire as the function has ended early, and is returning data

1 Like

Ok, sorry I accidentally added the return when replacing this my bad! Do I need to add the assert if i have the if statement? I also dont want to halt the script, i just want to error the pcall() so success returns false.

Pcalls (or protected calls) do not error, nor will they log errors to the console.
As for your DataStore issue, the DataStore will always return nil if no data exists for the given key.

local success, datastoreval = pcall(function()	
	return levelsUnlockedDataStore:GetAsync("Level0Unlocked"..playerID)
end)

if success then --Ensure no errors have occurred
	if datastoreval then
		--run success code here
	else
		levelsUnlockedDataStore:SetAsync("Level0Unlocked"..playerID, true)
	end
else 
	warn("Data found! Data Retrieved:", datastoreval)
end
1 Like

Ok yes! This is exactly what I want to happen and how I want the code to be formatted. But even though the datastore has no data the pcall still returns successful.

pcall() returns successful if it never encounters any errors, if it encounters an error, it will be unseccessful

1 Like

But if GetAsync has no datastore to get i thought it would error anyway?

Success is defined if no error has occurred within the pcall’s operation. DataStore calls are usually wrapped in pcalls since DataStores can error.

In simpler terms, success is whether or not the request to DatastoreService was a success, and datastoreval is the returned value OR error message from the pcall

1 Like

GetAsync will return successful if it as able to access it, if it returns nil, that has to do with the Data (or DataStore), not the pcall() as it thinks it was a success in accessing the DataStores and getting the Player’s Data

GetAsync can fail, and if it does, the pcall() will be unsuccessful, which is why people usually run it in a loop to access the Data, if that still fails, they usually kick the Player or prompt the Player with a warning, this is also the case with Saving Data.

1 Like

Hello, how can I help you today?

I know how the pcall and the datastore works, but for some reason, when the data store key has no value the pcal returns successful. I delete the key data, I dont make new keydata. And the pcall is still successful. GetAsync isnt erroring and I dont know why?

I said why here, If GetAsync was Successful, the pcall() will think its a success, this is different when it returns data as i stated here:

Are you saving the Player’s Data, Are you Applying the Data for the Player?
And are you creating Data for a new Player with no Data?

1 Like

The DataStore will not error unless there’s some kind of issue with retrieving the data from Roblox’s servers, usually during a service outage.
By design, GetAsync will always return nil if no data for the given key is present.

2 Likes

Ok, just to clarify, this game isn’t public. Im not using the data for anything yet. Im just testing how i will save. Im not saving nil to it. Have you heard of the plugin datastore editor? Im using that to remove all the data from the “level0Unlocked(PlayerID)” key. So there should be no save at all. Get Async should thrwo an error because there is no data saved to that key. If I force the pcall into an error by using error(). The if statement should save new data if the pcall errors which it does. Then if I play the game again, it sees that data and gives me the data instead of nil.

Really? I thought that the datastore would error if there was no found data.

Thats not true, it will throw an error if GetAsync fails to access Data

if you really wanted to:

if Success then
    if not data then -- if data is nil
       error()
    end
end
-- or:

   assert(data) -- Data is nil, therefore will error
2 Likes

Ohh, ok. Thank you both so much! I really appreciate the help! Sorry, it was a bit difficult
I changed the title incase anyone makes the same stupid mistake I did and comes looking for an answer.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.