SetAsync not working, and thus data isn't saving?

local Tries = 3
print(1)
-- Save data
repeat
	Success, Error = pcall(function()
		DataStore:SetAsync(player.UserId, PlayerData)
	end)
	print(2)
	TryCount += 1
until TryCount >= Tries or Success
print(Success, Error)

When I leave it prints 1, but nothing else. Even if I sit for 5 minutes after leaving, nothing new prints in the output…

The server closed while you are saving the data, thus the data didn’t save correctly. I suggest using game:BindToClose() to wait until the data is saved.

I do…

function DataManager.Save(player)	
	SetData(player) -- Save data
	PlotManager.Save(player)
	PlotManager.Clear(player)
	print("DONE SAVE") -- prints, even tho the prints in SetData don't
end

game:BindToClose(function()
	for i, player in next, Players:GetPlayers() do
		if player then
			SetData(player) -- Save data incase server shutsdown
		end
	end
end)

SetData function is what I have in the OP

You need to add a while wait(1) do loop until the all data is saved. Here is a demo script:

local usingDataStore = {}

-- SetAsync
usingDataStore[player.Name] = true
datastore:SetAsync(...)
if saving_is_completed then
	usingDataStore[player.Name] = nil
end


game:BindToClose(function()
	while next(usingDataStore) ~= nil do
		print("Still using data stores")
		wait(0.1)
	end
end)
1 Like

How can I still use the BindToClose function to save data when a server closes tho??

Roblox gives you a 30 second window to do things before it force shutdown the server for you, and you can use the 30 seconds to your advantage.