if not Settings.AutoAssignTeams then local t=Instance.new("\084\101\097\109",Teams)t.TeamColor=BC.new("\087\104\105\116\101")t.Name="\070\111\114 \072\105\114\101"end for t,s in pairs((script.Parent:WaitForChild("\084\121\099\111\111\110\115")):GetChildren())do Tycoons[s.Name]=s:Clone()if returnColorTaken(s.TeamColor)then local t repeat wait()t=BC.Random()until returnColorTaken(t)==false s.TeamColor.Value=t end local P=Instance.new("\084\101\097\109",Teams)P.Name=s.Name P.TeamColor=s.TeamColor.Value if not Settings.AutoAssignTeams then P.AutoAssignable=false end end function getPlrTycoon(t)for s,P in pairs(script.Parent.Tycoons:GetChildren())do if P:IsA("\077\111\100\101\108")then if P.Owner.Value==t then return P end end end return nil end game.Players.PlayerAdded:connect(function(t)local s=t:WaitForChild("\108\101\097\100\101\114\115\116\097\116\115")local P=Instance.new("\066\111\111\108\086\097\108\117\101")P.Name="\067\097\110\082\101\098\105\114\116\104"P.Value=false P.Parent=t if RebirthSettings.Rebirth.enabled==true then if s~=nil then local P=Instance.new("\078\117\109\098\101\114\086\097\108\117\101")P.Name="\082\101\098\105\114\116\104\115"P.Parent=s local G=PlayerStatManager:getStat(t,"\082\101\098\105\114\116\104\067\111\117\110\116")if G~=nil then P.Value=G else P.Value=0 end end end end)game.Players.PlayerRemoving:connect(function(t)PlayerStatManager:saveOnExit(t)PlayerStatManager:setPlayerSessionDataToNil(t)local s=game.ServerStorage.PlayerMoney:FindFirstChild(t.Name)if s~=nil then s:Destroy()end print("\065\070\084\069\082 \083\097\118\105\110\103 \097\110\100 \100\101\108\101\116\105\110\103 \112\108\114\083\116\097\116\115")local P=getPlrTycoon(t)if P then local t=Tycoons[P.Name]:Clone()P:Destroy()wait()t.Parent=script.Parent.Tycoons end end)
--run this first so if there is a 'white' team it is switched over
if not Settings["AutoAssignTeams"] then
	local teamHire = Instance.new("Team", Teams)
	teamHire.TeamColor = BC.new("White")
	teamHire.Name = "For Hire"
end

for i, v in pairs(script.Parent:WaitForChild("Tycoons"):GetChildren()) do
	Tycoons[v.Name] = v:Clone() -- Store the tycoons then make teams depending on the tycoon names
	if returnColorTaken(v.TeamColor) then
		--//Handle duplicate team colors
		local newColor
		repeat
			wait()
			newColor = BC.Random()
		until returnColorTaken(newColor) == false
		v.TeamColor.Value = newColor
	end
	--Now that there are for sure no duplicates, make your teams
	local NewTeam = Instance.new("Team", Teams)
	NewTeam.Name = v.Name
	NewTeam.TeamColor = v.TeamColor.Value
	if not Settings["AutoAssignTeams"] then
		NewTeam.AutoAssignable = false
	end
	--v.PurchaseHandler.Disabled = false
end

function getPlrTycoon(player)
	for i, v in pairs(script.Parent.Tycoons:GetChildren()) do
		if v:IsA("Model") then
			if v.Owner.Value == player then
				return v
			end
		end
	end
	return nil
end

game.Players.PlayerAdded:connect(function(player)
	local leaderStat = player:WaitForChild("leaderstats")
	local canRebirth = Instance.new("BoolValue")
	canRebirth.Name = "CanRebirth"
	canRebirth.Value = false
	canRebirth.Parent = player

	if RebirthSettings.Rebirth.enabled == true then
		if leaderStat ~= nil then
			local rebirth = Instance.new("NumberValue")
			rebirth.Name = "Rebirths"
			rebirth.Parent = leaderStat

			local rebirthCount = PlayerStatManager:getStat(player, "RebirthCount")

			if rebirthCount ~= nil then
				rebirth.Value = rebirthCount
			else
				rebirth.Value = 0
			end
		end
	end
end)

game.Players.PlayerRemoving:connect(function(player)
	--print("PlayerStatManager:saveOnExit("..player.Name..")")

	PlayerStatManager:saveOnExit(player)
	PlayerStatManager:setPlayerSessionDataToNil(player)
	local plrStats = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
	if plrStats ~= nil then
		plrStats:Destroy()
	end
	print("AFTER Saving and deleting plrStats")

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