SetAsync stopping script indefinitely

Hello everyone!
I am making a data saving system for my game, that works for getting the data but I have hit a wall when saving data.

This is the code I am using for saving the data:

Code
game.Players.PlayerRemoving:Connect(function(player)
    local SavingPlayerData = {}

    print("Player removing")
    local key = "Player-"..player.UserId
    local UnlocksFolder = player.UnlocksFolder:GetChildren()

    for i, v in pairs(UnlocksFolder) do
        savingPlayerData[v.Name] = v.Value
    end
    local EncodingDataString = HttpService:JSONEncode(SavingPlayerData)

    print("About to save")
    local success, err = pcall(function()
        print("Saving")
        DataStore:SetAsync(key, EncodingDataString)
        print("Saved")
    end)
    print("After save")

    if success then
        print("Save Success")
    else
        print("Data not saved")
    end
end)

As the player leaves the game, the code runs up until the part where it uses DataStore:SetAsync(key,EncodingDataString) and prints all of the prints before. However, it refuses to pass this point. Nothing is printed after the SetAsync.

Is there anything that I’m doing wrong in this?
Thanks in advance :smile:

There is no need to use JSONEncode Roblox will handle the table conversion for you.

1 Like

Still no fix o.o

There is no :BindToClose in this script. No errors are presented in the output, the script it seemingly stopping dead.

Just trying new things :smile:

iirc studio shuts down the server before the SetAsync request can be made. You should save data on BindToClose anyway as it prevents data loss for when a server is shutdown.

2 Likes

Thank you. I was testing in studio not the game. Wasn’t aware that SetAsync doesn’t run when studio closes without BindToClose.
Thanks! :smile:

1 Like