local moneymanagement = game:GetService("ServerStorage"):WaitForChild("MoneyStorage")
local DevAura = game:GetService("ServerStorage"):WaitForChild("Assets"):WaitForChild("Auras"):WaitForChild("Developers")
local DevHat = DevAura:WaitForChild("Cylinder"):Clone()
local DevGlow = DevAura:WaitForChild("GlowSmoke"):Clone()
local DevLines = DevAura:WaitForChild("Lines"):Clone()
local DevWings = DevAura:WaitForChild("Part"):Clone()
local bodyWeld = DevWings:FindFirstChild("BodyWeld")
local headWeld = DevHat:FindFirstChild("HeadWeld")

local tags = {
	Developers = {"pvrplehouse", "pvrplehouseDev", "imthebiggestbird2584"},
	Staff = {},
}
local tycoonBackups = {}


-- Define the getTycoonFromPlayer function within the module
local function getTycoonFromPlayer(player)
	for _, model in workspace.Tycoons:GetChildren() do
		if model:IsA("Model") then
			if model.Owner.Value == player then
				return model
			end
		end
	end
	return nil
end

-- Initialize tycoon backups
local function initializeTycoonBackups()
	for _, model in workspace.Tycoons:GetChildren() do
		if model:IsA("Model") then
			tycoonBackups[model.Name] = model:Clone()
		end
	end
end

initializeTycoonBackups()

game.Players.PlayerAdded:connect(function(player)
	local startingMoney = 0
	if player.Name == "pvrplehouse" then
		startingMoney = 500
	end
	
	local stats = Instance.new("IntValue")
	stats.Name = "leaderstats"
	stats.Parent = player

	local cash = Instance.new("IntValue")
	cash.Value = startingMoney
	cash.Name = "Cash"
	cash.Parent = stats

	local cashmoney = Instance.new("NumberValue")
	cashmoney.Value = cash.Value
	cashmoney.Name = player.Name
	cashmoney.Parent = moneymanagement
	

	local isowner = Instance.new("BoolValue")
	isowner.Name = "OwnsTycoon"
	isowner.Parent = cashmoney

	local kills = Instance.new("IntValue")
	kills.Value = 0
	kills.Name = "KOs"
	kills.Parent = stats

	local deaths = Instance.new("IntValue")
	deaths.Value = 0
	deaths.Name = "Wipeouts"
	deaths.Parent = stats
	
	local playerCash = game.ServerStorage.MoneyStorage:FindFirstChild(player.Name)
	if playerCash ~= nil then
		playerCash.Changed:connect(function()
			cash.Value = playerCash.Value
		end)
	end

	if player and table.find(tags.Developers, player.Name) then
		player:SetAttribute("isVIP")
		local character = player.Character or player.CharacterAdded:Wait()
		for _, part in character:GetDescendants() do
			if part:IsA("BasePart") then
				DevGlow.Parent = part
				DevLines.Parent = part
			end
		end
		DevWings.Parent = character.Torso
		DevHat.Parent = character.Head
		if bodyWeld then
			bodyWeld.Part1 = character.Torso
			bodyWeld.C1 = CFrame.new(0, -3, 0)
		end
		if headWeld then
			headWeld.Part1 = character.Head
			headWeld.C0 = CFrame.new(0, -1.2, -0.2)
		end
	end

	player.CharacterRemoving:Connect(function(character)
		task.defer(character.Destroy, character)
		task.wait()
		character:Destroy()
	end)
end)

game.Players.PlayerRemoving:connect(function(player)
	local cashmoney = moneymanagement:FindFirstChild(player.Name)
	if cashmoney ~= nil then
		cashmoney:Destroy()
	end

	local tycoon = getTycoonFromPlayer(player)
	if tycoon then
		local backup = tycoonBackups[tycoon.Name]:Clone()
		tycoon:Destroy()
		task.wait()
		backup.Parent = script.Parent.Tycoons
	end

	task.defer(player.Destroy, player)
	task.wait()
	player:Destroy()
end)


Tycoon backups's purposes was to regen the tycoons when the player left, but no regen is happening.