local function M(M)if sessionData[M]then local n,e=pcall(function()print("\077\111\110\101\121 --\062 \098\101\102\111\114\101 \083\101\116\065\115\121\110\099 ",sessionData[M].Money)playerData:SetAsync(M,sessionData[M])print("\077\111\110\101\121 --\062 \065\102\116\101\114 \083\101\116\065\115\121\110\099 ",sessionData[M].Money)wait()end)if not n then warn("\067\097\110\110\111\116 \115\097\118\101 \100\097\116\097 \102\111\114 \112\108\097\121\101\114\033--------------------------------------\062")warn("----------------------\115\097\118\101\080\108\097\121\101\114\068\097\116\097----------------------------------------------")warn("\087\104\097\116 \105\115 \116\104\101 \114\101\097\115\111\110 ",e)end end end function PlayerStatManager.getPlayerData(n,M)local e="\080\108\097\121\101\114\095"..M.UserId local a,F=pcall(function()print("\071\111\116 \112\108\097\121\101\114 \100\097\116\097----------------------\062")return sessionData[e]end)if a==false then warn("\069\082\082\079\082---------\080\108\097\121\101\114\083\116\097\116\077\097\110\097\103\101\114\058\103\101\116\080\108\097\121\101\114\068\097\116\097-----------\062")print(F)return nil else return F end end function PlayerStatManager.getStat(e,M,n)local a,F=pcall(function()local e="\080\108\097\121\101\114\095"..M.UserId return sessionData[e][n]end)if a==false then warn("\069\082\082\079\082---------\080\108\097\121\101\114\083\116\097\116\077\097\110\097\103\101\114\058\103\101\116\083\116\097\116-----------\062")print(F)return nil else return F end end function PlayerStatManager.saveStat(a,M,n,e)local F,Y=pcall(function()local a="\080\108\097\121\101\114\095"..M.UserId sessionData[a][n]=e end)if F==false then warn("\069\082\082\079\082---------\080\108\097\121\101\114\083\116\097\116\077\097\110\097\103\101\114\058\115\097\118\101\083\116\097\116-----------\062")print(Y)end end function PlayerStatManager.getStatInTycoonPurchases(e,M,n)local a,F=pcall(function()local e="\080\108\097\121\101\114\095"..M.UserId return sessionData[e].TycoonPurchases[n]end)if a==false then warn("\069\082\082\079\082---------\080\108\097\121\101\114\083\116\097\116\077\097\110\097\103\101\114\058\103\101\116\084\121\099\111\111\110\080\117\114\099\104\097\115\101\115-----------\062")print(F)return nil else return F end end function PlayerStatManager.setPlayerSessionDataToNil(n,M)if M~=nil then local n="\080\108\097\121\101\114\095"..M.UserId if sessionData[n]then sessionData[n]=nil end end end
-- Function to save player's data
local function savePlayerData(playerUserId)
	if sessionData[playerUserId] then
		local success, data = pcall(function()
			--NOTE!!! adding this print adds save if remove then save is lost
			print("Money --> before SetAsync ", sessionData[playerUserId]["Money"])
			playerData:SetAsync(playerUserId, sessionData[playerUserId])
			print("Money --> After SetAsync ", sessionData[playerUserId]["Money"])
			wait()
		end)
		if not success then
			warn("Cannot save data for player!-------------------------------------->")
			warn("----------------------savePlayerData----------------------------------------------")
			warn("What is the reason ", data)
		end
	end
end

function PlayerStatManager:getPlayerData(player)
	local playerUserId = "Player_" .. player.UserId
	local succes, data = pcall(function()
		print("Got player data---------------------->")
		return sessionData[playerUserId]
	end)
	if succes == false then
		warn("ERROR---------PlayerStatManager:getPlayerData----------->")
		print(data)
		return nil
	else
		return data
	end
end

function PlayerStatManager:getStat(player, statName)
	local succes, data = pcall(function()
		local playerUserId = "Player_" .. player.UserId
		return sessionData[playerUserId][statName]
	end)
	if succes == false then
		warn("ERROR---------PlayerStatManager:getStat----------->")
		print(data)
		return nil
	else
		return data
	end
end

function PlayerStatManager:saveStat(player, statName, value)
	local succes, err = pcall(function()
		local playerUserId = "Player_" .. player.UserId
		sessionData[playerUserId][statName] = value
	end)
	if succes == false then
		warn("ERROR---------PlayerStatManager:saveStat----------->")
		print(err)
	end
end

function PlayerStatManager:getStatInTycoonPurchases(player, statName)
	local succes, data = pcall(function()
		local playerUserId = "Player_" .. player.UserId
		return sessionData[playerUserId]["TycoonPurchases"][statName]
	end)
	if succes == false then
		warn("ERROR---------PlayerStatManager:getTycoonPurchases----------->")
		print(data)
		return nil
	else
		return data
	end
end

function PlayerStatManager:setPlayerSessionDataToNil(player)
	if player ~= nil then
		local playerUserId = "Player_" .. player.UserId
		if sessionData[playerUserId] then
			sessionData[playerUserId] = nil
		end
	end
end
