local function v(v,S)local f=main.pd[v]if f and(f.DataToUpdate or S)then return(main:GetModule("\068\097\116\097\083\116\111\114\101\115")):DataStoreRetry(datastoreRetries,function()return playerDataStore:UpdateAsync(v.UserId,function(S)local H=S or{DataKey=0}if f and main.pd[v]then if f.DataKey==H.DataKey then main.pd[v].DataToUpdate=false main.pd[v].DataKey=f.DataKey+1 local S={}for v,f in pairs(main.pd[v])do S[v]=f end if not S.SaveRank then S.Rank=0 end local u=dataNotToSave()for v,f in pairs(u)do S[f]=nil end H=S else H=nil end end return H end)end)end end

local function savePlayerData(player, playerLeaving)
	local pdata = main.pd[player]
	if pdata and (pdata.DataToUpdate or playerLeaving) then
		return main:GetModule("DataStores"):DataStoreRetry(datastoreRetries, function()
			return playerDataStore:UpdateAsync(player.UserId, function(oldValue)
				local newValue = oldValue or {DataKey = 0}
				if pdata and main.pd[player] then
					if pdata.DataKey == newValue.DataKey then
						main.pd[player].DataToUpdate = false
						main.pd[player].DataKey = pdata.DataKey + 1
						--
						local pdataToSave = {}
						for a,b in pairs(main.pd[player]) do
							pdataToSave[a] = b
						end
						if not pdataToSave.SaveRank then
						--if playerLeaving and not pdataToSave.SaveRank then
							pdataToSave.Rank = 0
						end
						local dataNotToSave = dataNotToSave()
						for i, v in pairs(dataNotToSave) do
							pdataToSave[v] = nil
						end
						--
						newValue = pdataToSave
					else
						newValue = nil --Prevent data being overwritten
					end
				end
				return newValue
			end)
		end)
	end
end


