Why isn't this "Success" condition of this datastore function running?

I was testing out an open-sourced script of an advanced datastore system that I found and all was going great until it came to running the code. The script is supposed to print the initial value of the player’s “money” upon joining the game, then when updated, it’ll print the updated value. But the issue is that nothing gets printed at all and I have traced the problem to the pcall success condition in this function.

local PlayerData = DataStoreService:GetDataStore("PlayerData")
local function SetUpPlayerData(Player) -- Setting up Player Data
	local PlayerID = Player.UserId
	
	local Data 
	local Success = pcall(function()
		Data = PlayerData:GetAsync(PlayerID)
	end)
	
	print("Testing 1")

	**if Success then**
**		if Data then**
**			print("Testing 2")**
**			SessionData[PlayerID] = Data**
**			print(SessionData[PlayerID].Money)**
**		else**
**			print("Testing 3")**
**			SessionData[PlayerID] = {Money = 0, Inventory = {}}**
**		end**
	end
end

“Testing 1” Gets printed but “Testing 2” and “Testing 3” doesn’t. What could be the issue? The script worked fine in the demo video but it doesn’t seem to be working at all right now? Could this be due to the DataStore v2 that got rolled out?

I don’t know if this is the issue but I think your missing a variable in your pcall.

You mean the error variable? Well I’ve tried it and that doesn’t seem to be the issue, whatever that isn’t “Successful” should get caught in the “else” block.

Could be because you’re running in studio. Either allow studio to access API or testplay it in a real game.

Try re-adding the “error” part of the pcall, then do

if Error then
      warn(Error)
end

Try adding the words print (“test”) into each part of the script to know what part to focus on. That usually works to help troubleshooting

They did do this print method lol

If you are talking about adding even more, that’s not needed in as the stuff between Test 1 and Test 2 is very basic.

However I would suggest actually printing Success to see what it contains. But my leading theory is that they just forgot to enable Studio Access API.

Ok, then I’ll try to analyze it and figure out what is wrong now.

Edit: I can’t see what is wrong. I might to figure out later but rn I am away from my desk and on my phone.

local PlayerData = DataStoreService:GetDataStore("PlayerData")

local function SetUpPlayerData(Player) -- Setting up Player Data
	local PlayerID = Player.UserId
	
	local Success, Result = pcall(function()
		return PlayerData:GetAsync(PlayerID)
	end)
	
	print("Testing 1")

	if Success then
		if Result then
			print("Testing 2")
			SessionData[PlayerID] = Result
			print(SessionData[PlayerID].Money)
		else
			print("Testing 3")
			SessionData[PlayerID] = {Money = 0, Inventory = {}}
		end
	end
end

I made a practical adjustment to your pcall, but anyway the most likely issue is that the GetAsync call is erroring. So if Success never goes through, you could error whenever success isn’t true, e.g:

if Success then
    -- 'Result' == data here
else
    error(Result)
end

and then try and fix what causes the error message. (E.g studio access not being enabled)

Hello guys, thank you all for the replies. As it turns out, the issue of the success condition not running was indeed due to the studio API access not being enabled(Credits to zeus and DevRyan). However, the script still isn’t working as demonstrated in the video. Though I suppose that is irrelevant to what I initially asked.

1 Like