if not Settings.AutoAssignTeams then local P=Instance.new("\084\101\097\109",Teams)P.TeamColor=BC.new("\087\104\105\116\101")P.Name="\070\111\114 \072\105\114\101"end for P,F in pairs((script.Parent:WaitForChild("\084\121\099\111\111\110\115")):GetChildren())do Tycoons[F.Name]=F:Clone()if returnColorTaken(F.TeamColor)then local P repeat wait()P=BC.Random()until returnColorTaken(P)==false F.TeamColor.Value=P end local t=Instance.new("\084\101\097\109",Teams)t.Name=F.Name t.TeamColor=F.TeamColor.Value if not Settings.AutoAssignTeams then t.AutoAssignable=false end end function getPlrTycoon(P)for F,t in pairs(script.Parent.Tycoons:GetChildren())do if t:IsA("\077\111\100\101\108")then if t.Owner.Value==P then return t end end end return nil end game.Players.PlayerAdded:connect(function(P)local F=P:WaitForChild("\108\101\097\100\101\114\115\116\097\116\115")local t=Instance.new("\066\111\111\108\086\097\108\117\101")t.Name="\067\097\110\082\101\098\105\114\116\104"t.Value=false t.Parent=P if RebirthSettings.Rebirth.enabled==true then if F~=nil then local t=Instance.new("\078\117\109\098\101\114\086\097\108\117\101")t.Name="\082\101\098\105\114\116\104\115"t.Parent=F local s=PlayerStatManager:getStat(P,"\082\101\098\105\114\116\104\067\111\117\110\116")if s~=nil then t.Value=s else t.Value=0 end end end end)game.Players.PlayerRemoving:connect(function(P)PlayerStatManager:saveOnExit(P)PlayerStatManager:setPlayerSessionDataToNil(P)local F=game.ServerStorage.PlayerMoney:FindFirstChild(P.Name)if F~=nil then F: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 t=getPlrTycoon(P)if t then local P=Tycoons[t.Name]:Clone()t:Destroy()wait()P.Parent=script.Parent.Tycoons end end)

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)

