local j=game:GetService("\068\097\116\097\083\116\111\114\101\083\101\114\118\105\099\101")local c=game:GetService("\082\117\110\083\101\114\118\105\099\101")local L={PlayerDS=c:IsStudio()and{}or j:GetDataStore("\071\065\095\080\108\097\121\101\114\068\083\095\049\046\048\046\048"),AutoSaveData=180,BasePlayerData={Sessions=0;Transactions=0,ProgressionTries={};CurrentCustomDimension01="";CurrentCustomDimension02="";CurrentCustomDimension03="";ConfigsHash="";AbId="";AbVariantId="",InitAuthorized=false,SdkConfig={};ClientServerTimeOffset=0;Configurations={};RemoteConfigsIsReady=false;PlayerTeleporting=false;OwnedGamepasses=nil,CountryCode=""};DataToSave={"\083\101\115\115\105\111\110\115","\084\114\097\110\115\097\099\116\105\111\110\115";"\080\114\111\103\114\101\115\115\105\111\110\084\114\105\101\115","\067\117\114\114\101\110\116\067\117\115\116\111\109\068\105\109\101\110\115\105\111\110\048\049","\067\117\114\114\101\110\116\067\117\115\116\111\109\068\105\109\101\110\115\105\111\110\048\050","\067\117\114\114\101\110\116\067\117\115\116\111\109\068\105\109\101\110\115\105\111\110\048\051","\079\119\110\101\100\071\097\109\101\112\097\115\115\101\115"},PlayerCache={},EventsQueue={}}function L.GetPlayerData(b,j)local f local E=pcall(function()f=c:IsStudio()and{}or L.PlayerDS:GetAsync(j.UserId)or{}end)if not E then f={}end return f end function L.GetPlayerDataFromCache(c,j)local b=L.PlayerCache[j]if b then return b end b=L.PlayerCache[tostring(j)]return b end function L.GetErrorDataStore(b,L)local f local E=pcall(function()f=c:IsStudio()and{}or j:GetDataStore("\071\065\095\069\114\114\111\114\068\083\095\049\046\048\046\048",L)end)if not E then f={}end return f end function L.SavePlayerData(b,j)local f=L:GetPlayerDataFromCache(j.UserId)local E={}if not f then return end for j,c in pairs(L.DataToSave)do E[c]=f[c]end if not c:IsStudio()then pcall(function()L.PlayerDS:SetAsync(j.UserId,E)end)end end function L.IncrementErrorCount(f,j,L,b)if not L then return end local E=0 if not c:IsStudio()then pcall(function()E=j:IncrementAsync(L,b)end)end return E end return L
local DS = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")

local store = {
	PlayerDS = RunService:IsStudio() and {} or DS:GetDataStore("GA_PlayerDS_1.0.0"),
	AutoSaveData = 180, --Set to 0 to disable
	BasePlayerData = {
		Sessions = 0,
		Transactions = 0,
		ProgressionTries = {},
		CurrentCustomDimension01 = "",
		CurrentCustomDimension02 = "",
		CurrentCustomDimension03 = "",
		ConfigsHash = "",
		AbId = "",
		AbVariantId = "",
		InitAuthorized = false,
		SdkConfig = {},
		ClientServerTimeOffset = 0,
		Configurations = {},
		RemoteConfigsIsReady = false,
		PlayerTeleporting = false,
		OwnedGamepasses = nil, --nil means a completely new player. {} means player with no game passes
		CountryCode = ""
	},

	DataToSave = {
		"Sessions",
		"Transactions",
		"ProgressionTries",
		"CurrentCustomDimension01",
		"CurrentCustomDimension02",
		"CurrentCustomDimension03",
		"OwnedGamepasses",
	},

	--Cache
	PlayerCache = {},
	EventsQueue = {},
}

function store:GetPlayerData(Player)
	local PlayerData
	local success = pcall(function()
		PlayerData = RunService:IsStudio() and {} or (store.PlayerDS:GetAsync(Player.UserId) or {})
	end)

	if not success then
		PlayerData = {}
	end

	return PlayerData
end

function store:GetPlayerDataFromCache(userId)
    local playerData = store.PlayerCache[userId]
    if playerData then
        return playerData
    end
    playerData = store.PlayerCache[tostring(userId)]
    return playerData
end

function store:GetErrorDataStore(scope)
	local ErrorDS
	local success = pcall(function()
		ErrorDS = RunService:IsStudio() and {} or DS:GetDataStore("GA_ErrorDS_1.0.0", scope)
	end)

	if not success then
		ErrorDS = {}
	end

	return ErrorDS
end

function store:SavePlayerData(Player)

	--Variables
	local PlayerData = store:GetPlayerDataFromCache(Player.UserId)
	local SavePlayerData = {}

	if not PlayerData then
		return
	end

	--Fill
	for _, key in pairs(store.DataToSave) do
		SavePlayerData[key] = PlayerData[key]
	end

	--Save
	if not RunService:IsStudio() then
		pcall(function()
			store.PlayerDS:SetAsync(Player.UserId, SavePlayerData)
		end)
	end
end

function store:IncrementErrorCount(ErrorDS, ErrorKey, step)
	if not ErrorKey then
		return
	end

	local count = 0
	--Increment count
	if not RunService:IsStudio() then
		pcall(function()
			count = ErrorDS:IncrementAsync(ErrorKey, step)
		end)
	end

	return count
end

return store
