local autoModel = _G.Storage.Assets.Autos:WaitForChild("Auto")
local planeModel = _G.Storage.Assets.Vehicles:WaitForChild("BP-10")
local purchaseModule = require(game.ServerScriptService.PurchaseModule)
teams = {}
function init(parent)
	wait(1)
	local m = parent
	local team = game.Teams[parent.Parent.Name]
	local autoSpawn = parent:WaitForChild("AutoSpawn")
	local planeSpawn = parent:WaitForChild("PlaneSpawn")

	local function lockPlane(plane)
		plane.Pilot.Anchored = true
		plane.Pilot.Changed:Connect(function(name)
			if name == "Occupant" then
				if plane.Pilot.Occupant then wait(3) plane.Pilot.Anchored = false end
			end
		end)
	end
	
	if not teams[team.Name] then teams[team.Name] = 0 end
	
	local function spawnAuto()
		local property = game.Workspace.Property[team.Name] 
		local newAuto = autoModel:Clone()
		newAuto.Parent = property
		newAuto.PrimaryPart.CFrame = autoSpawn.CFrame + Vector3.new(0,2,0)
		newAuto["Body Colors"].TorsoColor = team.TeamColor

		local planeModel = planeModel:Clone()
		planeModel.Parent = property
		planeModel:SetPrimaryPartCFrame(planeSpawn.CFrame)
		--lockPlane(planeModel)
		
		purchaseModule.colorVehicle(planeModel,team)
		purchaseModule.collisionCheck(planeModel,0.5,10)
		
		newAuto.Humanoid.Died:Connect(function()
			wait(3)
			planeModel:Destroy()
			newAuto:Destroy()
			teams[team.Name]-=1
		end)
		
		teams[team.Name] += 1

	end
	
	while parent.Parent do
		if not (teams[team.Name] < _G.Storage.Variables.AutoNumber.Value) then wait(30) continue end
		teams[team.Name] = 0
		for _,object in pairs(game.Workspace.Property[team.Name]:GetChildren()) do
			if object.Name == "Auto" and object.PrimaryPart and object.PrimaryPart.Velocity.Magnitude > 30 then -- and object.PrimaryPart.Velocity.Magnitude > 50 then
				teams[team.Name]+=1
			end
		end
		_G.AutoTeams = teams
		--print("Auto: ",team.Name,teams[team.Name])
		spawnAuto()
		wait(30)
	end
end
return getfenv()