DataStore Tables Not Saving

I’m trying to save tables using datastore for a Pokemon-type game. This is what I came up with, but for some reason it doesn’t save. No errors appeared in output. Any idea why? Any help is appreciated.

local datastoreService = game:GetService("DataStoreService")
local leadKyatchiStore = datastoreService:GetDataStore("leadKyatchiStoreTEST")
local caughtKyatchiStore = datastoreService:GetDataStore("caughtKyatchiTEST")
local httpService = game:GetService("HttpService")

function saveData(plr,stat,datastore)
    datastoreService:GetDataStore(datastore):SetAsync(plr,datastore)
end

game.Players.PlayerAdded:Connect(function(player)
    local Folder = Instance.new("Folder",player)
    Folder.Name = "playerData"

    local leadKyatchi = Instance.new("StringValue",Folder)
    leadKyatchi.Name = "leadKyatchi"

    local caughtKyatchi = Instance.new("Folder",player)
    caughtKyatchi.Name = "caughtKyatchi"

    local Data = caughtKyatchiStore:GetAsync(player.UserId)
    if Data then
        local tables = httpService:JSONDecode(Data)
        for name,value in ipairs(tables) do
            local stat = Instance.new("BoolValue",caughtKyatchi)
            stat.Name = name
            stat.Value = value
        end
    else
        print("Player is new. Start off as Natom.")
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local datatosave = {}
    for i,v in ipairs(player.caughtKyatchi:GetChildren()) do
        datatosave[v.Name] = v.Value
    end
    caughtKyatchiStore:SetAsync(player.UserId,httpService:JSONEncode(datatosave))
end)

If you’re testing it with only player, it’s likely that the server is at its terminating procedures before the data is saved. SetAsync() yields, so you could use a debounce variable (say saving=true) for when it’s saving data. And bind the server’s shutdown to a function that yields to that debounce saving=false with the function BindToClose()

Hope that helps,
Also cool use of JSONEncode, but its probably also good to know that regardless of what lua type is passed into SetAsync(), all values in roblox datastores are stored as strings encoded in JSON.
You might also want to incorporate pcall() with your datastore functions, because datastore methods can have a variety of errors due to connection problems or other things.