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