local g=Instance.new("\080\097\114\116")g.Locked=true g.BackSurface=3 g.BottomSurface=3 g.FrontSurface=3 g.LeftSurface=3 g.RightSurface=3 g.TopSurface=3 g.Size=Vector3.new(1,1,4)g.BrickColor=BrickColor.new(24);(script.Parent.RocketScript:clone()).Parent=g;(script.Parent.Explosion:clone()).Parent=g;(script.Parent.Swoosh:clone()).Parent=g function getPlayerTeamName(g)local n=game.Teams:children()for D=1,#n,1 do if n[D].TeamColor==g.TeamColor then return n[D].Name end end end function getTeamColorFromName(g)local n=game.Teams:children()for D=1,#n,1 do if n[D].Name==g then return n[D].TeamColor end end return BrickColor.new(24)end function fire(n)local D=n-sphere.Position D=computeDirection(D)local t=g:clone()local C=sphere.Position local w=C+D*8 t.CFrame=CFrame.new(w,w+D)t.RocketScript.Disabled=false t.Parent=game.Workspace end function computeDirection(g)local n=g.magnitude*g.magnitude local D=1/math.sqrt(n)return Vector3.new(g.x*D,g.y*D,g.z*D)end function scanForHumans()local g=game.Players:children()local n=10000 local D=nil for t=1,#g,1 do if getPlayerTeamName(g[t])~=script.Parent.OwningTeam.Value then if g[t].Character~=nil then local C=(g[t].Character.PrimaryPart.Position-sphere.Position).magnitude if C<n then n=C D=g[t].Character end end end end myTarget=D end function moveKillbot()local g=math.random(-100,100)local n=math.random(-40,40)local D=math.random(-100,100)if sphere.Position.y<10 and n<0 then n=-n end if sphere.Position.y>80 and n>0 then n=-n end if myTarget~=nil then local n=computeDirection(myTarget.PrimaryPart.Position-sphere.Position)g=g+n.x*80 D=D+n.z*80 end local t=computeDirection(Vector3.new(g,n,D))sphere.BodyPosition.position=sphere.Position+t*40 end function onTouched(g)local n=g.Parent:findFirstChild("\072\117\109\097\110\111\105\100")if n~=nil then n.Health=n.Health-2000 else script.Parent.Parent=nil end end sphere.Touched:connect(onTouched)if script.Parent.OwningTeam.Value~=nil then sphere.BrickColor=getTeamColorFromName(script.Parent.OwningTeam.Value)end while true do scanForHumans()for g=1,5,1 do if myTarget~=nil then if math.random(1,3)==2 then fire(myTarget.PrimaryPart.Position)end wait(math.random(1,2))moveKillbot()wait(math.random(1,2))end end wait(1)end

local Rocket = Instance.new("Part")
Rocket.Locked = true
Rocket.BackSurface = 3
Rocket.BottomSurface = 3
Rocket.FrontSurface = 3
Rocket.LeftSurface = 3
Rocket.RightSurface = 3
Rocket.TopSurface = 3
Rocket.Size = Vector3.new(1,1,4)
Rocket.BrickColor = BrickColor.new(24)
script.Parent.RocketScript:clone().Parent = Rocket
script.Parent.Explosion:clone().Parent = Rocket
script.Parent.Swoosh:clone().Parent = Rocket

function getPlayerTeamName(player)
	local teams = game.Teams:children()
	for i=1, #teams do
		if (teams[i].TeamColor == player.TeamColor) then return teams[i].Name end
	end
end

function getTeamColorFromName(name)
	local teams = game.Teams:children()
	for i=1, #teams do
		if (teams[i].Name == name) then return teams[i].TeamColor end
	end

	return BrickColor.new(24)
end

function fire(target)


	local dir = target - sphere.Position
	dir = computeDirection(dir)


	local missile = Rocket:clone()

	local spawnPos = sphere.Position

	local pos = spawnPos + (dir * 8)
	
	--missile.Position = pos
	missile.CFrame = CFrame.new(pos,  pos + dir)

	
	missile.RocketScript.Disabled = false
	missile.Parent = game.Workspace


end

function computeDirection(vec)
	local lenSquared = vec.magnitude * vec.magnitude
	local invSqrt = 1 / math.sqrt(lenSquared)
	return Vector3.new(vec.x * invSqrt, vec.y * invSqrt, vec.z * invSqrt)
end



function scanForHumans()
	-- KILL THE HUMANS!!!!!!!!!!

	-- find the closest human that is not on my team
	

	local players = game.Players:children()

	local best_distance = 10000
	local best_target = nil

	for n=1,#players do
		if (getPlayerTeamName(players[n]) ~= script.Parent.OwningTeam.Value) then
			if (players[n].Character ~= nil) then
				local dist = (players[n].Character.PrimaryPart.Position - sphere.Position).magnitude
				if (dist < best_distance) then
					best_distance = dist
					best_target = players[n].Character
				end
			end
		end
	end

	myTarget =  best_target

	
end

function moveKillbot()
	
	-- killbots move using a biased random walk toward the target
	-- they also like to float at least 10 studs above the ground

	local dx = math.random(-100,100)
	local dy = math.random(-40,40)
	local dz = math.random(-100,100)

	if (sphere.Position.y < 10 and dy < 0) then dy = -dy end
	if (sphere.Position.y > 80 and dy > 0) then dy = -dy end

	if (myTarget ~= nil) then
		local dir = computeDirection(myTarget.PrimaryPart.Position - sphere.Position)
		dx = dx + (dir.x * 80) -- change this number to alter player trophism
		dz = dz + (dir.z * 80)
	end

	local vec = computeDirection(Vector3.new(dx,dy,dz))

	sphere.BodyPosition.position = sphere.Position + (vec * 40) -- change this number to alter speed
	

end

function onTouched(hit)
	local humanoid = hit.Parent:findFirstChild("Humanoid")
	if humanoid~=nil then
		humanoid.Health = humanoid.Health - 2000 -- Killbots kill you when you touch them! duh.
	else
		-- sadly, there is no good way to give a killbot hit points, so one hit with any weapon kills them
		script.Parent.Parent = nil
	end
end

sphere.Touched:connect(onTouched)

if (script.Parent.OwningTeam.Value ~= nil) then sphere.BrickColor = getTeamColorFromName(script.Parent.OwningTeam.Value) end

while true do  -- loop forever
	scanForHumans()

	for n=1,5 do
		if (myTarget ~= nil) then
			if(math.random(1,3) == 2) then fire(myTarget.PrimaryPart.Position) end
			wait(math.random(1,2))
			moveKillbot()
			wait(math.random(1,2))
		end
	end
	wait(1) -- don't hog CPU
end
