Datastore not saving

I wrote a quick datastore that loads player data into a folder and saves at the end of the session by converting the folders into tables

Now the issue is that stuff like PlayerRemoving or BindToClose do not want to yield or the data just does not want to save even though everything is working except the SetAsync.

local function CreateTableToSave(Table)
	local DataToReturn = {}

	local function HasValue(Object)
		local ValueCheck = pcall(function()
			Object.Value = Object.Value
		end)
		return ValueCheck
	end

	for _,Object in pairs(Table) do
		if Object:IsA('Folder') then
			DataToReturn[Object.Name] = CreateTableToSave(Object:GetChildren())	
		elseif HasValue(Object) then
			DataToReturn[Object.Name] = Object.Value
		end
		task.wait(.1)
	end

	task.wait(.1)
	return DataToReturn
end


function SavePlayer(plr)
	local PlayerData = ReplicatedStorage.PlayerData:FindFirstChild(plr.Name)
	local Completed = false
	if not PlayerData  then print("No Data") return end
	if CURRENTLYSAVING[plr] then print("Already Saving") return end
	
	CURRENTLYSAVING[plr.Name] = true

	local DataToSave = CreateTableToSave(PlayerData:GetChildren())
	MainData:SetAsync(plr.UserId,DataToSave)
	Completed = true

	CURRENTLYSAVING[plr.Name] = nil
	return PlayerData
end



game.Players.PlayerRemoving:Connect(function(plr)
	SavePlayer(plr):Destroy()
	task.wait(1) -- Force it to yield
end)

game:BindToClose(function()
	for _,PlrFolder in pairs(game.ReplicatedStorage.PlayerData:GetChildren()) do 		
		task.spawn(function()
			SavePlayer(game.Players:FindFirstChild(PlrFolder.Name))
		end)
	end
end)

why are you doing the destroy here?
also do you get any errors?

also you need to remove all these waits there isn’t a reason for the this is why it never returns the data as a table to save before the player leaves

also if you just did GetDescendants on the main folder and then in this forloop did if Object:IsA(‘Folder’) then continue end at the top it would only give you the other decendants below or values if thats all that is in any of the folders

No errors, and plus made it print the data and it was all fine but the issue is it not saving/waiting for it to finish saving

For anyone in the future with similar issue ots because i was using SetAsync try to use something more reliable like UpdateAsync