:SetAsync seems not to work in studio

Hello!

I’ve run into another issue, when I am doing :SetAsync(), it seems not to work.

function saveData(player)
    local key = 'PLAYERDATA_'..player.UserId
    local playerFolder = player:FindFirstChild('PlayerData')
    
    function convertInstanceToData(currentFolder)
        local returnedData = {}
        for i,v in pairs(currentFolder:GetChildren()) do
            if v:IsA('Folder') then
                returnedData[v.Name] = convertInstanceToData(v)
            else
                returnedData[v.Name] = v.Value
            end
        end
        return returnedData
    end
    
    local constructedTable = convertInstanceToData(playerFolder)
    
    print(constructedTable)
    
    local successful, errorMessage = pcall(function()
        dataStore:SetAsync(key, constructedTable)
    end)
    
    print(tostring(errorMessage))
end

players.PlayerRemoving:Connect(saveData)

game:BindToClose(function()
    for i, player in pairs(players:GetPlayers()) do
        coroutine.wrap(saveData)(player)
    end
end)

Outputs:

 {
                    ["Coins"] = "0",
                    ["Completed Obbies"] = "0",
                    ["Inventory"] =  ▼  {
                       ["Accessories"] = {},
                       ["Gear"] = {},
                       ["Packages"] = {}
                    },
                    ["Wins"] = "0"
                 } 

which is what I expect. However, when I rejoin and print the data, it’s always nil.

players.PlayerAdded:Connect(function(player)
    local key = 'PLAYERDATA_'..player.UserId
    
    local data
    
    local successful, errorMessage = pcall(function()
        data = dataStore:GetAsync(key)
    end)
    print(data) -- nil
    if successful then
        if not data then
            data = defaultData
        end
        verifyData(data, player)
    else
        print(tostring(errorMessage))
    end
end)

Edit: Tested in an actual game and it works, but :SetAsync(), I just can’t get it to work in studio.

Does it work in a normal server? If so, enable API requests for studio via game settings

1 Like

API services are enabled and it still seems not to work, strange.

You are creating a new thread by using coroutines to save player data. Try removing the coroutines in BindToClose. When a server is shutting down, it waits until any yields have ended. Creating a new thread prevents a yield from being created when saving data.

2 Likes