local function S(S)local z=main.sd[S]return(main:GetModule("\068\097\116\097\083\116\111\114\101\115")):DataStoreRetry(datastoreRetries,function()return systemDataStore:UpdateAsync(S,function(J)local U=J or dataTemplate(S)if z then if z.DataKey==U.DataKey then U.DataToUpdate=false U.DataKey=U.DataKey+1 for S,z in pairs(z)do if S=="\082\101\099\111\114\100\115\084\111\065\100\100"and#z>0 then for S,z in pairs(z)do table.insert(U.Records,z)end elseif(S=="\082\101\099\111\114\100\115\084\111\082\101\109\111\118\101"or S=="\082\101\099\111\114\100\115\084\111\077\111\100\105\102\121")and#z>0 then for z,J in pairs(z)do local K=(main:GetModule("\099\102")):FindUserIdInRecord(U.Records,J.UserId)if K then table.remove(U.Records,K)if S=="\082\101\099\111\114\100\115\084\111\077\111\100\105\102\121"then table.insert(U.Records,J)else end end end elseif S~="\082\101\099\111\114\100\115"and(S~="\068\097\116\097\075\101\121"and S~="\068\097\116\097\084\111\085\112\100\097\116\101")then U[S]=z end end main.sd[S]=U else U=nil end end return U end)end)end
--Save data to DataStore
local function saveSystemData(key)
	local sdata = main.sd[key]
	return main:GetModule("DataStores"):DataStoreRetry(datastoreRetries, function()
		return systemDataStore:UpdateAsync(key, function(oldValue)
			local newValue = oldValue or dataTemplate(key)
			if sdata then
				if sdata.DataKey == newValue.DataKey then
					newValue.DataToUpdate = false
					newValue.DataKey = newValue.DataKey + 1
					for statName, statValue in pairs(sdata) do
						if statName == "RecordsToAdd" and #statValue > 0 then
							for i, record in pairs(statValue) do
								--print("Datastore: "..key.." | AddRecord | "..record.UserId)
								table.insert(newValue.Records, record)
							end
						elseif (statName == "RecordsToRemove" or statName == "RecordsToModify") and #statValue > 0 then
							for i, record in pairs(statValue) do
								local recordToRemovePos = main:GetModule("cf")
									:FindUserIdInRecord(newValue.Records, record.UserId)
								if recordToRemovePos then
									table.remove(newValue.Records, recordToRemovePos)
									if statName == "RecordsToModify" then
										--print("Datastore: "..key.." | ModifyRecord | "..record.UserId)
										table.insert(newValue.Records, record)
									else
										--print("Datastore: "..key.." | RemoveRecord | "..record.UserId)
									end
								end
							end
						elseif statName ~= "Records" and statName ~= "DataKey" and statName ~= "DataToUpdate" then
							newValue[statName] = statValue
						end
					end
					main.sd[key] = newValue
				else
					newValue = nil
				end
			end
			return newValue
		end)
	end)
end
