local ServerScriptService = game:GetService("ServerScriptService")
local TycoonsFolder = workspace:WaitForChild("Tycoons")
local MoneyStorage = game:GetService("ServerStorage"):WaitForChild("MoneyStorage")

-- Helper function to get the player from a hit
local function getPlayerFromHit(hit)
	return game.Players:GetPlayerFromCharacter(hit.Parent)
end

-- Helper function to check if the player owns the tycoon
local function playerOwnsTycoon(player, tycoonModel)
	return player and tycoonModel.Owner.Value == player
end

-- Helper function to check if the character is alive
local function characterIsAlive(character)
	local humanoid = character:FindFirstChild("Humanoid")
	return humanoid and humanoid.Health > 0
end

-- Helper function to get the player's cash storage
local function getCashStorage(player)
	return MoneyStorage:FindFirstChild(player.Name)
end

-- Function to set up a tycoon
local function setupTycoon(tycoonModel)
	local objects = {}
	local teamcolor = BrickColor.new(tycoonModel.Name)

	local TycoonSSFolder = Instance.new("Folder")
	TycoonSSFolder.Name = tycoonModel.Name
	TycoonSSFolder.Parent = game.ServerStorage.Assets.Tycoons
	
	tycoonModel:WaitForChild("Buttons")
	tycoonModel:WaitForChild("Purchases")
	local Essentials = tycoonModel:WaitForChild("Essentials")
	local Cash = tycoonModel:WaitForChild("Cash")
	local PurchasedObjects = tycoonModel:WaitForChild("PurchasedObjects")

	-- Connect to the collector touch event
	Essentials.Collector.Touched:Connect(function(hit)
		local cash = hit:FindFirstChild("Cash")
		if cash then
			Cash.Value += cash.Value
			game.Debris:AddItem(hit, 0)
		end
	end)

	-- Connect to the giver touch event
	Essentials.Giver.Touched:Connect(function(hit)
		local player = getPlayerFromHit(hit)
		if player and playerOwnsTycoon(player, tycoonModel) and characterIsAlive(hit.Parent) then
			local cashStorage = getCashStorage(player)
			if cashStorage then
				cashStorage.Value += Cash.Value
				Cash.Value = 0
			end
		end
	end)
	


	-- Set up each button in the tycoon
	for _, button in tycoonModel.Buttons:GetChildren() do
		local head = button:FindFirstChild("Head")
		if head then
			local objectName = button.Object and button.Object.Value
			local purchaseObject = objectName and tycoonModel.Purchases:FindFirstChild(objectName)
			if purchaseObject then
				objects[objectName] = purchaseObject:Clone()
				purchaseObject.Parent = TycoonSSFolder
			else
				warn("Button: " .. button.Name .. " is missing its object and has been removed.")
				head.CanCollide = false
				head.Transparency = 1
			end

			if button:FindFirstChild("Dependency") then
				head.CanCollide = false
				head.Transparency = 1
				coroutine.wrap(function()
					while not PurchasedObjects:FindFirstChild(button.Dependency.Value) do
						task.wait()
					end
					head.CanCollide = true
					head.Transparency = 0
				end)()
			end

			-- Connect to the button head touch event
			head.Touched:Connect(function(hit)
				local player = getPlayerFromHit(hit)
				if player and head.CanCollide and playerOwnsTycoon(player, tycoonModel) and characterIsAlive(hit.Parent) then
					local cashStorage = getCashStorage(player)
					if cashStorage and cashStorage.Value >= button.Price.Value then
						cashStorage.Value -= button.Price.Value
						head.Transparency = 1
						head.CanCollide = false
						objects[objectName].Parent = PurchasedObjects
					end
				end
			end)
		end
	end
end


for _, tycoon in TycoonsFolder:GetChildren() do
	setupTycoon(tycoon)
end

-- Set up newly added tycoons
TycoonsFolder.ChildAdded:Connect(setupTycoon)


ive been thinking. all of the tycoons will use the same assets from the "Purchases" model. so can i just move them myself in ServerStorage and have the scripts take them from there instead? show me how to modify.