local P=require(script.Parent:WaitForChild("\080\108\097\121\101\114\083\116\097\116\077\097\110\097\103\101\114"))local O=script.Parent:WaitForChild("\084\121\099\111\111\110\115")local Q=O:GetChildren()local G=game:GetService("\083\101\114\118\101\114\083\116\111\114\097\103\101")local v=G:WaitForChild("\080\108\097\121\101\114\077\111\110\101\121")local T=script.PurchaseHandlerNew local n=script.GateControlNew local j=game:GetService("\080\108\097\121\101\114\115")local U={}local a=require(script.Parent.Settings)function hideButton(P,O)local Q=O:GetChildren()local G=nil for O=1,#Q,1 do if Q[O].Object.Value==P then G=Q[O]end end if G~=nil then local P=G:FindFirstChild("\072\101\097\100")if P~=nil then if a.ButtonsFadeOut then P.CanCollide=false coroutine.resume(coroutine.create(function()for O=1,20,1 do wait(a.FadeOutTime/20)P.Transparency=P.Transparency+.05 end end))else P.CanCollide=false P.Transparency=1 end end end end for O=1,#Q,1 do Q[O].PurchaseHandler:Destroy()Q[O].Entrance["\084\111\117\099\104 \116\111 \099\108\097\105\109\033"].GateControl:Destroy();(T:Clone()).Parent=Q[O]local G=n:Clone()G.Parent=Q[O].Entrance["\084\111\117\099\104 \116\111 \099\108\097\105\109\033"]G.Disabled=false local j=Q[O]:FindFirstChild("\080\117\114\099\104\097\115\101\100\079\098\106\101\099\116\115")local a=Q[O].Purchases local x=Q[O]:FindFirstChild("\079\119\110\101\114")local F=Q[O].Entrance local p=(F:GetChildren())[1].Head U[O]=require(Q[O].PurchaseHandlerNew)local y=true p.Touched:Connect(function(G)local T=G.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")if T~=nil and y then local G=v:FindFirstChild(T.Parent.Name)local n=nil if G~=nil then n=G:FindFirstChild("\079\119\110\115\084\121\099\111\111\110")end if n~=nil and n.Value==nil then p.Transparency=1 y=false local G=game.Players:FindFirstChild(T.Parent.Name)if G~=nil then local v=P:getPlayerData(G)for P,O in pairs(U[O])do if v[P]==true then O.Parent=j end end wait(3)for P,G in pairs(U[O])do if v[P]==true then local G=P local v=Q[O].Buttons hideButton(G,v)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 j~=nil and x~=nil then j.ChildAdded:Connect(function(O)local Q=x.Value if Q~=nil then local G=P:getStat(Q,O.Name)if Q~=nil and G==false then P:ChangeStat(Q,O.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 O.ChildAdded:Connect(function(O)local Q=O:FindFirstChild("\080\117\114\099\104\097\115\101\072\097\110\100\108\101\114")if Q~=nil then Q:Destroy();(T:Clone()).Parent=O local G=O:FindFirstChild("\080\117\114\099\104\097\115\101\100\079\098\106\101\099\116\115")local n=O.Purchases local j=O:FindFirstChild("\079\119\110\101\114")local U=O.Entrance local a=(U:GetChildren())[1].Head local x=require(O.PurchaseHandlerNew)local F=true a.Touched:Connect(function(Q)local T=Q.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")if T~=nil and F then local Q=v:FindFirstChild(T.Parent.Name)local n=nil if Q~=nil then n=Q:FindFirstChild("\079\119\110\115\084\121\099\111\111\110")end if n~=nil and n.Value==nil then a.Transparency=1 F=false local Q=game.Players:FindFirstChild(T.Parent.Name)if Q~=nil then local v=P:getPlayerData(Q)for P,O in pairs(x)do if v[P]==true then O.Parent=G end end wait(3)for P,Q in pairs(x)do if v[P]==true then local Q=P local G=O.Buttons hideButton(Q,G)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 G~=nil and j~=nil then G.ChildAdded:Connect(function(O)local Q=j.Value if Q~=nil then local G=P:getStat(Q,O.Name)if Q~=nil and G==false then P:ChangeStat(Q,O.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 O=j:GetChildren()for Q=1,#O,1 do if O[Q]~=nil then local G=v:FindFirstChild(O[Q].Name)if G then P:saveStat(O[Q],"\077\111\110\101\121",G.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
