Problem with saving system, HELP

i’m trying to make a saving system using data store but i have multiple issues going on here and i have no idea why they are happening :

  • sometimes it saves and sometimes it doesn’t, with no error message or exceeding the limit

  • when it does save, all the code below the pcall function does not run, nothing gets printed as if there was a return but there isn’t

i was using update async and thought it was the return that stopped the rest of the function but set async does the same problem

it’s been a week i tried everything and nothing works, and now i’m a bit in a burnout because of this

any help will be greatly appreaciated thank you!!

here is the important part of the code :

function loadData(plr : Player)
	local data = {}
	
	local succ, err = pcall(function()
		data = dataStore:GetAsync(plr.UserId)
	end)
	
	local folder = toFolder(loadTable(dataDefault, data))
	folder.Parent = plr
	folder.Name = "Data"
	
	print("loaded version "..data.SaveVersion, data)
	
	if not succ then
		plr:AddTag("LoadError")
		warn("Error while loading "..plr.Name.."'s data : "..err)
	end
	plr:AddTag("Loaded")
	return succ
end

function saveData(plr : Player)
	
	if recentlySavedPlayers[plr] then
		return
	else
		recentlySavedPlayers[plr] = true
		task.spawn(function()
			task.wait(saveDebounce)
			recentlySavedPlayers[plr] = nil
		end)
	end
	
	local data = toTable(plr.Data)
	data.SaveVersion += 1
	
	print("saving as version "..data.SaveVersion.."...", data)
	
	local succ, err = pcall(function()
		dataStore:SetAsync(plr.UserId, data)
	end)
	
	warn("SAVED") -- does not print for some reason
	
	if not succ then
		warn("Error while saving "..plr.Name.."'s data : "..err)
	end
	return succ
end

Try adding a BindToClose() function.

local runService = game:GetService("RunService")
local players = game:GetService("Players")

game:BindToClose(function()
    if runService:IsStudio() then task.wait(3) return nil end
    for _, player in ipairs(players:GetPlayers()) do
        saveData(player)
    end
    task.wait(3)
end)

By the way, definitely use UpdateAsync().

i already have one, this is a module script and i’m firing the functions with a server script