local J={}local s=game:GetService("\084\101\097\109\115")local p=require(script.Parent.Settings)local Z=BrickColor local d=Instance.new("\070\111\108\100\101\114",game.ServerStorage)d.Name="\080\108\097\121\101\114\077\111\110\101\121";(Instance.new("\077\111\100\101\108",workspace)).Name="\080\097\114\116\083\116\111\114\097\103\101"function returnColorTaken(J)for s,p in pairs(s:GetChildren())do if p:IsA("\084\101\097\109")then if p.TeamColor==J then return true end end end return false end if not p.AutoAssignTeams then local J=Instance.new("\084\101\097\109",s)J.TeamColor=Z.new("\087\104\105\116\101")J.Name="\070\111\114 \072\105\114\101"end for d,t in pairs((script.Parent:WaitForChild("\084\121\099\111\111\110\115")):GetChildren())do J[t.Name]=t:Clone()if returnColorTaken(t.TeamColor)then local J repeat wait()J=Z.Random()until returnColorTaken(J)==false t.TeamColor.Value=J end local F=Instance.new("\084\101\097\109",s)F.Name=t.Name F.TeamColor=t.TeamColor.Value if not p.AutoAssignTeams then F.AutoAssignable=false end t.PurchaseHandler.Disabled=false end function getPlrTycoon(J)for s,p in pairs(script.Parent.Tycoons:GetChildren())do if p:IsA("\077\111\100\101\108")then if p.Owner.Value==J then return p end end end return nil end game.Players.PlayerAdded:connect(function(J)local s=Instance.new("\078\117\109\098\101\114\086\097\108\117\101",game.ServerStorage.PlayerMoney)s.Name=J.Name local p=Instance.new("\079\098\106\101\099\116\086\097\108\117\101",s)p.Name="\079\119\110\115\084\121\099\111\111\110"end)game.Players.PlayerRemoving:connect(function(s)local p=game.ServerStorage.PlayerMoney:FindFirstChild(s.Name)if p~=nil then p:Destroy()end local Z=getPlrTycoon(s)if Z then local s=J[Z.Name]:Clone()Z:Destroy()wait()s.Parent=script.Parent.Tycoons end end)
--[[
	All configurations are located in the "Settings" Module script.
	Please don't edit this script unless you know what you're doing.
--]]

local Tycoons = {}
local Teams = game:GetService('Teams')
local Settings = require(script.Parent.Settings)
local BC = BrickColor
local Storage = Instance.new('Folder', game.ServerStorage)
Storage.Name = "PlayerMoney"
Instance.new('Model',workspace).Name = "PartStorage" --  parts dropped go in here to be killed >:)

function returnColorTaken(color)
	for i,v in pairs(Teams:GetChildren()) do
		if v:IsA('Team') then
			if v.TeamColor == color then
				return true
			end
		end
	end
	return false
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 plrStats = Instance.new("NumberValue",game.ServerStorage.PlayerMoney)
	plrStats.Name = player.Name
	local isOwner = Instance.new("ObjectValue",plrStats)
	isOwner.Name = "OwnsTycoon"
end)

game.Players.PlayerRemoving:connect(function(player)
	local plrStats = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
	if plrStats ~= nil then
		plrStats:Destroy()
	end
	local tycoon = getPlrTycoon(player)
	if tycoon then
		local backup = Tycoons[tycoon.Name]:Clone()
		tycoon:Destroy()
		wait()
		backup.Parent=script.Parent.Tycoons
	end
end)
