function PlayerStatManager.ChangeStat(t,R,N,p)local C="\080\108\097\121\101\114\095"..R.UserId sessionData[C][N]=p end function PlayerStatManager.ChangeStatInTycoonPurchases(t,R,N,p)local C="\080\108\097\121\101\114\095"..R.UserId sessionData[C].TycoonPurchases[N]=p print("\083\097\118\105\110\103------------------\062 ",N)end function PlayerStatManager.IncrementStat(t,R,N,p)local C="\080\108\097\121\101\114\095"..R.UserId if typeof(sessionData[C][N])=="\110\117\109\098\101\114"then sessionData[C][N]=sessionData[C][N]+p end end local function R(R)local N=serverStorage:WaitForChild("\080\108\097\121\101\114\077\111\110\101\121")local p=N:FindFirstChild(R.Name)if p==nil then p=Instance.new("\078\117\109\098\101\114\086\097\108\117\101",N)p.Name=R.Name local t=Instance.new("\079\098\106\101\099\116\086\097\108\117\101",p)t.Name="\079\119\110\115\084\121\099\111\111\110"end local t="\080\108\097\121\101\114\095"..R.UserId local C,X=pcall(function()return playerData:GetAsync(t)end)if C then if X then print("----------\077\111\110\101\121\058 ---------\062 "..X.Money)print("\083\104\111\119 \116\121\099\111\111\110 \112\117\114\099\104\097\115\101\115---------------------")sessionData[t]=X for R,N in pairs(sessionData[t].TycoonPurchases)do print(R,N)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 p=N:FindFirstChild(R.Name)if p then p.Value=p.Value+X.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 ",R.Name)wait()local p=0 local C,X=pcall(function()if R:IsInGroup(SaveSettings.giveCashToPlayersInGroup.groupId)then local t=N:FindFirstChild(R.Name)if t then t.Value=t.Value+SaveSettings.giveCashToPlayersInGroup.cashAmount p=t.Value end end end)if C==false then print(X)end sessionData[t]={}sessionData[t].Money=p sessionData[t].RebirthCount=0 sessionData[t].TycoonPurchases={}end p.Changed:Connect(function(R)print("\112\108\097\121\101\114\083\116\097\116\115\046\067\104\097\110\103\101\100 ",R)sessionData[t].Money=R end)else warn("\067\111\117\108\100\110\'\116 \103\101\116 \100\097\116\097")print(X)end end
-- Function that other scripts can call to change a player's stats
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
