function PlayerStatManager.ChangeStat(I,v,B,Q)local u="\080\108\097\121\101\114\095"..v.UserId sessionData[u][B]=Q end function PlayerStatManager.ChangeStatInTycoonPurchases(I,v,B,Q)local u="\080\108\097\121\101\114\095"..v.UserId sessionData[u].TycoonPurchases[B]=Q print("\083\097\118\105\110\103------------------\062 ",B)end function PlayerStatManager.IncrementStat(I,v,B,Q)local u="\080\108\097\121\101\114\095"..v.UserId if typeof(sessionData[u][B])=="\110\117\109\098\101\114"then sessionData[u][B]=sessionData[u][B]+Q end end local function v(v)local B=serverStorage:WaitForChild("\080\108\097\121\101\114\077\111\110\101\121")local Q=B:FindFirstChild(v.Name)if Q==nil then Q=Instance.new("\078\117\109\098\101\114\086\097\108\117\101",B)Q.Name=v.Name local I=Instance.new("\079\098\106\101\099\116\086\097\108\117\101",Q)I.Name="\079\119\110\115\084\121\099\111\111\110"end local I="\080\108\097\121\101\114\095"..v.UserId local u,U=pcall(function()return playerData:GetAsync(I)end)if u then if U then print("----------\077\111\110\101\121\058 ---------\062 "..U.Money)print("\083\104\111\119 \116\121\099\111\111\110 \112\117\114\099\104\097\115\101\115---------------------")sessionData[I]=U for v,B in pairs(sessionData[I].TycoonPurchases)do print(v,B)end print("\069\110\100 \112\114\105\110\116\105\110\103 \116\121\099\111\111\110 \112\117\114\099\104\097\115\101\115-------------")local Q=B:FindFirstChild(v.Name)if Q then Q.Value=Q.Value+U.Money else print("------------\112\108\097\121\101\114\077\111\110\101\121\067\104\105\108\100 \105\115 \110\105\108")end else print("\115\101\116\117\112\080\108\097\121\101\114\068\097\116\097 \110\111 \100\097\116\097 \102\111\117\110\100 \102\111\114 \112\108\097\121\101\114 ",v.Name)wait()local Q=0 local u,U=pcall(function()if v:IsInGroup(SaveSettings.giveCashToPlayersInGroup.groupId)then local I=B:FindFirstChild(v.Name)if I then I.Value=I.Value+SaveSettings.giveCashToPlayersInGroup.cashAmount Q=I.Value end end end)if u==false then print(U)end sessionData[I]={}sessionData[I].Money=Q sessionData[I].RebirthCount=0 sessionData[I].TycoonPurchases={}end Q.Changed:Connect(function(v)print("\112\108\097\121\101\114\083\116\097\116\115\046\067\104\097\110\103\101\100 ",v)sessionData[I].Money=v end)else warn("\067\111\117\108\100\110\'\116 \103\101\116 \100\097\116\097")print(U)end end

function PlayerStatManager:ChangeStat(player, statName, value)
	local playerUserId = "Player_" .. player.UserId
	sessionData[playerUserId][statName] = value
end

function PlayerStatManager:ChangeStatInTycoonPurchases(player, statName, value)
	local playerUserId = "Player_" .. player.UserId
	sessionData[playerUserId]["TycoonPurchases"][statName] = value
	print("Saving------------------> ",statName)
end

function PlayerStatManager:IncrementStat(player, statName, value)
	local playerUserId = "Player_" .. player.UserId
	if typeof(sessionData[playerUserId][statName]) == "number" then
		sessionData[playerUserId][statName] = sessionData[playerUserId][statName] + value
	end
end


local function setupPlayerData(player)	
	local playerMoney = serverStorage:WaitForChild("PlayerMoney")
	local playerStats = playerMoney:FindFirstChild(player.Name)
	--Sometimes people have duplicate tycoon add e.g in core handler script
	--adds PlayerMoney to storage and number value with player name that holds the 
	--money value 
	if playerStats == nil then
		playerStats = Instance.new("NumberValue",playerMoney)
		playerStats.Name = player.Name
		local isOwner = Instance.new("ObjectValue",playerStats)
		isOwner.Name = "OwnsTycoon"
	end
	local playerUserId = "Player_" .. player.UserId
 
	local success,data = pcall(function()
		return playerData:GetAsync(playerUserId)
	end)
	
	if success then
		if data then
			print("----------Money: ---------> "..data["Money"])
			print("Show tycoon purchases---------------------")
			sessionData[playerUserId] = data
			for key,value in pairs(sessionData[playerUserId]["TycoonPurchases"]) do
				print(key,value)
			end
			print("End printing tycoon purchases-------------")
			
			local playerMoneyChild = playerMoney:FindFirstChild(player.Name)
			if playerMoneyChild then
				playerMoneyChild.Value = playerMoneyChild.Value + data["Money"]
			else print("------------playerMoneyChild is nil") end
			
		else
			print("setupPlayerData no data found for player ",player.Name)	
			wait()
			local money = 0
			local success,err = pcall(function()
				if player:IsInGroup(SaveSettings.giveCashToPlayersInGroup.groupId) then
					local plrCash = playerMoney:FindFirstChild(player.Name)
					if plrCash then
						plrCash.Value = plrCash.Value + SaveSettings.giveCashToPlayersInGroup.cashAmount
						money = plrCash.Value	
					end
				end
			end)
			
			if success == false then
				print(err)
			end
			
			sessionData[playerUserId] = {}
			sessionData[playerUserId]["Money"] = money
			sessionData[playerUserId]["RebirthCount"] = 0
			sessionData[playerUserId]["TycoonPurchases"] = {}
		end	
		playerStats.Changed:Connect(function(newValue) 
			print("playerStats.Changed ",newValue)
			sessionData[playerUserId]["Money"] = newValue
		end)
	else
		warn("Couldn't get data")
		print(data)
	end		
end
 