Error occurs when saving a list with SetAsync

In this code I am trying to save variables into a list. However, some errors occur while saving data. As I stated in the code, while I can get the “Before pcall” and “Inside pcall” outputs, I cannot get the other outputs. What could be the reason for this?

local function savePlayerData(player, playerCrowns)
    local dataToSave = {}

    for i = 1, 35 do -- I have 35 different crowns
        dataToSave["Crown"..i] = {Name = playerCrowns["Crown" .. i].Name,Value = playerCrowns["Crown" .. i].Value}
    end

    print("Before pcall") -- I can get output
    local success, err = pcall(function()
        print("Inside pcall") -- I can get output
        playerDataStore:SetAsync(tostring(player.UserId), dataToSave)
        print("After SetAsync") -- I can **NOT** get output
    end)
    print("After pcall") -- I can **NOT** get output

    if success then
        print("Saved") -- -- I can **NOT** get output
    elseif not success then
        warn("Failed to save data for player " .. player.Name .. ": " .. err) -- -- I can **NOT** get output
    end
end

game.Players.PlayerRemoving:Connect(function(player)
    savePlayerData(player,_G.PlayerCrowns[player])
end)

1 Like

Are you testing in studio? One reason could be that you actually have the StorageService disabled. This is not uncommon as it is disabled by default. But it should show such an error in the output. To check, check the output and go to Home > Game Settings > And activate “Enable studio access to API services”

1 Like

This setting is already enabled in my game.

Additionally, this code causes data to sometimes be saved and sometimes not. But I don’t know why.

Try testing outside Studio as it doesn’t work sometimes

Try using else instead of elseif and see what the error is

if success then
        print("Saved") -- -- I can **NOT** get output
    else 
        warn("Failed to save data for player " .. player.Name .. ": " .. err) -- -- I can **NOT** get output
    end

The reason for the error that I have been trying to solve for days is that I tried it in Studio, not on normal servers. Thanks…

Oh ok, understood! Change the script by adding a wait():

It could be that the async operation does not finish in time and the rest just keeps going. If that also does not work, move the functionality temporarily to a different trigger, e.g. a chat message or so, to be able to find the error. It could be that you leave the server before saving and/or the other print()-events and thus do not see their result. So use something where your character stays in the server and then see the error message.
// EDIT: Change the 3 to 2 or even 1 or less as it can cause lags.

1 Like

Thank you very much, my data is saved on the normal server, but how can I save my data in Studio server?

you could try game:BindToClose(), that is a function that fires when the server closes (you should also do that in your datasaving) ((and btw set up a for loop with a task.spawn function inside of it, since you need to save data as fast as possible, if the server were to close))

1 Like

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