local U=require(script.Parent:WaitForChild("\080\108\097\121\101\114\083\116\097\116\077\097\110\097\103\101\114"))local A=script.Parent:WaitForChild("\084\121\099\111\111\110\115")local O=A:GetChildren()local p=game:GetService("\083\101\114\118\101\114\083\116\111\114\097\103\101")local C=p:WaitForChild("\080\108\097\121\101\114\077\111\110\101\121")local I=script.PurchaseHandlerNew local c=script.GateControlNew local Q=game:GetService("\080\108\097\121\101\114\115")local H={}local X=require(script.Parent.Settings)function hideButton(U,A)local O=A:GetChildren()local p=nil for A=1,#O,1 do if O[A].Object.Value==U then p=O[A]end end if p~=nil then local U=p:FindFirstChild("\072\101\097\100")if U~=nil then if X.ButtonsFadeOut then U.CanCollide=false coroutine.resume(coroutine.create(function()for A=1,20,1 do wait(X.FadeOutTime/20)U.Transparency=U.Transparency+.05 end end))else U.CanCollide=false U.Transparency=1 end end end end for A=1,#O,1 do O[A].PurchaseHandler:Destroy()O[A].Entrance["\084\111\117\099\104 \116\111 \099\108\097\105\109\033"].GateControl:Destroy();(I:Clone()).Parent=O[A]local p=c:Clone()p.Parent=O[A].Entrance["\084\111\117\099\104 \116\111 \099\108\097\105\109\033"]p.Disabled=false local Q=O[A]:FindFirstChild("\080\117\114\099\104\097\115\101\100\079\098\106\101\099\116\115")local X=O[A].Purchases local u=O[A]:FindFirstChild("\079\119\110\101\114")local N=O[A].Entrance local v=(N:GetChildren())[1].Head H[A]=require(O[A].PurchaseHandlerNew)local z=true v.Touched:Connect(function(p)local I=p.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")if I~=nil and z then local p=C:FindFirstChild(I.Parent.Name)local c=nil if p~=nil then c=p:FindFirstChild("\079\119\110\115\084\121\099\111\111\110")end if c~=nil and c.Value==nil then v.Transparency=1 z=false local p=game.Players:FindFirstChild(I.Parent.Name)if p~=nil then local C=U:getPlayerData(p)for U,A in pairs(H[A])do if C[U]==true then A.Parent=Q end end wait(3)for U,p in pairs(H[A])do if C[U]==true then local p=U local C=O[A].Buttons hideButton(p,C)end end else print("\080\108\097\121\101\114 \061\061 \110\105\108")end else print("\111\119\110\115\084\121\099\111\111\110\046\086\097\108\117\101 ~\061 \110\105\108 \111\114 \111\119\110\115\084\121\099\111\111\110 \061\061 \110\105\108 ")end else print("\072\117\109\097\110\111\105\100 \061\061 \110\105\108 \111\114 \100\101\098\111\117\110\099\101 \105\115 \102\097\108\115\101")end end)if Q~=nil and u~=nil then Q.ChildAdded:Connect(function(A)local O=u.Value if O~=nil then local p=U:getStat(O,A.Name)if O~=nil and p==false then U:ChangeStat(O,A.Name,true)end end end)else warn("\083\097\118\101\084\114\105\103\103\101\114 \115\099\114\105\112\116 \119\111\110\'\116 \115\097\118\101 \100\097\116\097 \098\101\099\097\117\115\101 \101\105\116\104\101\114 \112\117\114\099\104\097\115\101\100\079\098\106\101\099\116\115 \111\114 \111\119\110\101\114 \105\115 \078\073\076")end end A.ChildAdded:Connect(function(A)local O=A:FindFirstChild("\080\117\114\099\104\097\115\101\072\097\110\100\108\101\114")if O~=nil then O:Destroy();(I:Clone()).Parent=A local p=A:FindFirstChild("\080\117\114\099\104\097\115\101\100\079\098\106\101\099\116\115")local c=A.Purchases local Q=A:FindFirstChild("\079\119\110\101\114")local H=A.Entrance local X=(H:GetChildren())[1].Head local u=require(A.PurchaseHandlerNew)local N=true X.Touched:Connect(function(O)local I=O.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")if I~=nil and N then local O=C:FindFirstChild(I.Parent.Name)local c=nil if O~=nil then c=O:FindFirstChild("\079\119\110\115\084\121\099\111\111\110")end if c~=nil and c.Value==nil then X.Transparency=1 N=false local O=game.Players:FindFirstChild(I.Parent.Name)if O~=nil then local C=U:getPlayerData(O)for U,A in pairs(u)do if C[U]==true then A.Parent=p end end wait(3)for U,O in pairs(u)do if C[U]==true then local O=U local p=A.Buttons hideButton(O,p)end end else print("\080\108\097\121\101\114 \061\061 \110\105\108")end else print("\111\119\110\115\084\121\099\111\111\110\046\086\097\108\117\101 ~\061 \110\105\108 \111\114 \111\119\110\115\084\121\099\111\111\110 \061\061 \110\105\108 ")end else print("\072\117\109\097\110\111\105\100 \061\061 \110\105\108 \111\114 \100\101\098\111\117\110\099\101 \105\115 \102\097\108\115\101")end end)if p~=nil and Q~=nil then p.ChildAdded:Connect(function(A)local O=Q.Value if O~=nil then local p=U:getStat(O,A.Name)if O~=nil and p==false then U:ChangeStat(O,A.Name,true)end end end)else warn("\083\097\118\101\084\114\105\103\103\101\114 \115\099\114\105\112\116 \119\111\110\'\116 \115\097\118\101 \100\097\116\097 \098\101\099\097\117\115\101 \101\105\116\104\101\114 \112\117\114\099\104\097\115\101\100\079\098\106\101\099\116\115 \111\114 \111\119\110\101\114 \105\115 \078\073\076")end end end)while wait(59)do pcall(function()local A=Q:GetChildren()for O=1,#A,1 do if A[O]~=nil then local p=C:FindFirstChild(A[O].Name)if p then U:saveStat(A[O],"\077\111\110\101\121",p.Value)end end end end)end


local PlayerStatManager = require(script.Parent:WaitForChild("PlayerStatManager"))

local tycoonsFolder = script.Parent:WaitForChild("Tycoons")
local tycoons = tycoonsFolder:GetChildren()
local serverStorage = game:GetService("ServerStorage")
local playerMoney = serverStorage:WaitForChild("PlayerMoney")
local purchaseHandlerNew = script.PurchaseHandlerNew
local gateControlNew = script.GateControlNew
local players = game:GetService("Players")

local allObjects = {}

local Settings = require(script.Parent.Settings)

function hideButton(name,buttons)
	local allButtons = buttons:GetChildren()
	local item = nil
	for i = 1,#allButtons,1 do
		if allButtons[i].Object.Value == name then
			item = allButtons[i]
		end
	end
	if item ~= nil then
		local head = item:FindFirstChild("Head")
		if head ~= nil then
			if Settings['ButtonsFadeOut'] then
				head.CanCollide = false
				coroutine.resume(coroutine.create(function()
					for i=1,20 do
						wait(Settings['FadeOutTime']/20)
						head.Transparency = head.Transparency + 0.05
					end
				end))
			else
				head.CanCollide = false
				head.Transparency = 1
			end
		end
	end
end

for i = 1,#tycoons,1 do
	tycoons[i].PurchaseHandler:Destroy()
	tycoons[i].Entrance["Touch to claim!"].GateControl:Destroy()
	purchaseHandlerNew:Clone().Parent = tycoons[i]
	
	local gateControlNewClone = gateControlNew:Clone()
	gateControlNewClone.Parent = tycoons[i].Entrance["Touch to claim!"]
	gateControlNewClone.Disabled = false
	
	local purchasedObjects = tycoons[i]:FindFirstChild("PurchasedObjects")
	local purchases = tycoons[i].Purchases
	local owner = tycoons[i]:FindFirstChild("Owner")
	local entrance = tycoons[i].Entrance
	local touchToClaim = entrance:GetChildren()[1].Head
	
	allObjects[i] = require(tycoons[i].PurchaseHandlerNew)
	
	--Load data
	local debounce = true
	touchToClaim.Touched:Connect(function(hit)
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		if humanoid ~= nil and debounce then
			local playerInPlayerMoney = playerMoney:FindFirstChild(humanoid.Parent.Name)
			local ownsTycoon = nil
			if playerInPlayerMoney ~= nil then
		   		 ownsTycoon = playerInPlayerMoney:FindFirstChild("OwnsTycoon")
			end
			if ownsTycoon ~= nil and ownsTycoon.Value == nil then
				touchToClaim.Transparency = 1
				debounce = false
				local player = game.Players:FindFirstChild(humanoid.Parent.Name)
				if player ~= nil then
					local data = PlayerStatManager:getPlayerData(player)
					for key,value in pairs(allObjects[i]) do	
						if data[key] == true then
							value.Parent = purchasedObjects
						end
					end	
					wait(3)
					for k,v in pairs(allObjects[i]) do
						if data[k] == true then
							local buttonName = k
							local buttons = tycoons[i].Buttons
							hideButton(buttonName,buttons)
						end
					end	
				else
					print("Player == nil")	
				end
			else
				print("ownsTycoon.Value ~= nil or ownsTycoon == nil ")	
			end
		else
			print("Humanoid == nil or debounce is false")		
		end
	end)
	
	--Save data
	if purchasedObjects ~= nil and owner ~= nil then
		purchasedObjects.ChildAdded:Connect(function(instance)
			local player = owner.Value
			if player ~= nil then
				local stat = PlayerStatManager:getStat(player,instance.Name)
				if player ~= nil and stat == false then
					PlayerStatManager:ChangeStat(player, instance.Name, true)
				end
			end
		end)
	else
		warn("SaveTrigger script won't save data because either purchasedObjects or owner is NIL")
	end
end

tycoonsFolder.ChildAdded:Connect(function(instance)
	local purchaseHandler = instance:FindFirstChild("PurchaseHandler")
	if purchaseHandler ~= nil then
		purchaseHandler:Destroy()
		purchaseHandlerNew:Clone().Parent = instance
		local purchasedObjects = instance:FindFirstChild("PurchasedObjects")
		local purchases = instance.Purchases
		local owner = instance:FindFirstChild("Owner")
		local entrance = instance.Entrance
		local touchToClaim = entrance:GetChildren()[1].Head
		
		local PurchaseHandlerNew = require(instance.PurchaseHandlerNew)
		
		--Load data
		local debounce = true
		touchToClaim.Touched:Connect(function(hit)
			local humanoid = hit.Parent:FindFirstChild("Humanoid")
			if humanoid ~= nil  and debounce then
				local playerInPlayerMoney = playerMoney:FindFirstChild(humanoid.Parent.Name)
				local ownsTycoon = nil
				if playerInPlayerMoney ~= nil then
					 ownsTycoon = playerInPlayerMoney:FindFirstChild("OwnsTycoon")
				end
				if ownsTycoon ~= nil and ownsTycoon.Value == nil then
					touchToClaim.Transparency = 1
					debounce = false
					local player = game.Players:FindFirstChild(humanoid.Parent.Name)
					if player ~= nil then
						local data = PlayerStatManager:getPlayerData(player)
						for key,value in pairs(PurchaseHandlerNew) do
							if data[key] == true then
								value.Parent = purchasedObjects
							end
						end	
						wait(3)
						for k,v in pairs(PurchaseHandlerNew) do
							if data[k] == true then
								local buttonName = k
								local buttons = instance.Buttons
								hideButton(buttonName,buttons)
							end
						end	
						
					else
						print("Player == nil")	
				 	end
				else
					print("ownsTycoon.Value ~= nil or ownsTycoon == nil ")	
				end
			else
				print("Humanoid == nil or debounce is false")		
			end
		end)
		
		--Save data
		if purchasedObjects ~= nil and owner ~= nil then
			purchasedObjects.ChildAdded:Connect(function(instance)
				local player = owner.Value
				if player ~= nil then
					local stat = PlayerStatManager:getStat(player,instance.Name)
					if player ~= nil and stat == false then
						PlayerStatManager:ChangeStat(player, instance.Name, true)
					end
				end
			end)
		else
			warn("SaveTrigger script won't save data because either purchasedObjects or owner is NIL")
		end
	end
end)

while wait(59) do
	pcall(function()
		local allPlayers = players:GetChildren()
		for i = 1,#allPlayers,1 do
			if allPlayers[i] ~= nil then
				local playersMoneyChild = playerMoney:FindFirstChild(allPlayers[i].Name)
				if playersMoneyChild then
					PlayerStatManager:saveStat(allPlayers[i], "Money", playersMoneyChild.Value)
				end
			end
		end
	end)
end

