-----------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------


dist=20

function followany()
following=true
	while following==true do

		local ch=game.Players:children()
		for i=1, #ch do
			local l=game.Workspace:findFirstChild(ch[i].Name)
			if l~=nil then
				local s=l.Torso
				local p=l.Torso.Position
				local q=script.Parent.Torso.Position
				local d=math.sqrt( ((p.x-q.x)^2)+((p.y-q.y)^2)+((p.z-q.z)^2) )
				if smallest==nil then
					smallest=d
					target=s
				elseif d<smallest then
					smallest=d
					target=s
				end
			end
		end

		if smallest==nil then stopmoving() return end
		
		if smallest>6*dist then return end
		
		script.Parent.Humanoid:MoveTo(target.Position, target)

		wait(0.5)
		for i=1, 6 do
			if target.Parent.Humanoid.Health<1 then stopmoving() else	wait(0.5) end
		end
	end
end



function gohome()
	script.Parent.Humanoid:MoveTo(Vector3.new(0,0,0), game.Workspace.Bases.Base)
end
function stopmoving()
	script.Parent.Humanoid:MoveTo(script.Parent.Torso.Position, script.Parent.Torso)
end

function follow(name)
	following=true

	local p=game.Workspace:findFirstChild(name)
	if p==nil then return end
	while following==true do	
		script.Parent.Humanoid:MoveTo(p.Torso.Position, p.Torso)
		wait(0.5)
		for i=1, 3 do
			if p.Humanoid.Health<1 then following=false; stopmoving() return end
			wait(0.5)
		end
	end
end

function attack(name)
	if script.Parent:findFirstChild("Sword")~=nil then	
		following=true
		local p=game.Workspace:findFirstChild(name)
		if p==nil then return end
		while following==true do
			script.Parent.Humanoid:MoveTo(p.Torso.Position, p.Torso)
			wait(0.5)
			for i=1, 3 do
				if p.Humanoid.Health<1 then following=false; stopmoving() return end
				local l=p.Torso.Position
				local q=script.Parent.Torso.Position
				local d=math.sqrt( ((l.x-q.x)^2)+((l.y-q.y)^2)+((l.z-q.z)^2) )
				if d<10 then
					script.Parent.Humanoid:MoveTo(p.Torso.Position, p.Torso); slash()
					if (q.y-l.y)>3 and (q.y-l.y)<7 then
						script.Parent.Humanoid:MoveTo(p.Torso.Position+Vector3.new(math.random(-4,4),0,math.random(-4,4)), p.Torso);
					end
				end
				wait(1)
			end
		end
	end
end

function attackany()
if script.Parent:findFirstChild("Sword")~=nil then	

	following=true
		while following==true do

			local ch=game.Players:children()
			for i=1, #ch do
				local l=game.Workspace:findFirstChild(ch[i].Name)
				if l~=nil then
					local s=l.Torso
					local p=l.Torso.Position
					local q=script.Parent.Torso.Position
					local d=math.sqrt( ((p.x-q.x)^2)+((p.y-q.y)^2)+((p.z-q.z)^2) )
					if smallest==nil then
						smallest=d
						target=s
					elseif d<smallest then
						smallest=d
						target=s
					end
				end
			end

			if smallest==nil then stopmoving() return end
			
			if smallest>6*dist then return end
			
			script.Parent.Humanoid:MoveTo(target.Position, target)

			if smallest<10 then
				slash()
			end
			if target.Parent==nil then stopmoving() return end
			wait(0.5)
			for i=1, 3 do
				if target.Parent.Humanoid.Health<1 then 
					stopmoving() 
				else
					local p=target.Position
					local q=script.Parent.Torso.Position
					local d=math.sqrt( ((p.x-q.x)^2)+((p.y-q.y)^2)+((p.z-q.z)^2) )
					if d<10 then
						script.Parent.Humanoid:MoveTo(target.Position, target); slash()
						if (q.y-p.y)>3 and (q.y-p.y)<7 then
							script.Parent.Humanoid:MoveTo(p.Torso.Position+Vector3.new(math.random(-4,4),0,math.random(-4,4)), p.Torso);
						end
					end
					wait(1)
				end
			end
		end
	end
end


function patrol()
	if points==nil then
		points=0
		if game.Workspace:findFirstChild("pp1")~=nil then
		pp1=game.Workspace:findFirstChild("pp1")
		local points=points+1
			if game.Workspace:findFirstChild("pp2")~=nil then
			pp2=game.Workspace:findFirstChild("pp2")
		local points=points+1
				if game.Workspace:findFirstChild("pp3")~=nil then
				pp3=game.Workspace:findFirstChild("pp3")
		local points=points+1
					if game.Workspace:findFirstChild("pp4")~=nil then
					pp4=game.Workspace:findFirstChild("pp4")
		local points=points+1
						if game.Workspace:findFirstChild("pp5")~=nil then
						pp5=game.Workspace:findFirstChild("pp5")
		local points=points+1
							if game.Workspace:findFirstChild("pp6")~=nil then
							pp6=game.Workspace:findFirstChild("pp6")
		local points=points+1
								if game.Workspace:findFirstChild("pp7")~=nil then
								pp7=game.Workspace:findFirstChild("pp7")
		local points=points+1
									if game.Workspace:findFirstChild("pp8")~=nil then
									pp8=game.Workspace:findFirstChild("pp8")
		local points=points+1
										if game.Workspace:findFirstChild("pp9")~=nil then
										pp9=game.Workspace:findFirstChild("pp9")
		local points=points+1
										end
									end
								end
							end
						end
					end
				end
			end
		end
	end

	if points<1 then return end
	patrolling=true
	local time=2
	if points==1 then return end
	if points>1 then
		while patrolling==true do
			script.Parent.Humanoid:MoveTo(pp1.Position, pp1)
			wait(time)
			script.Parent.Humanoid:MoveTo(pp2.Position, pp2)
			wait(time)
			if points>2 then
				script.Parent.Humanoid:MoveTo(pp3.Position, pp3)
				wait(time)
				if points>3 then
					script.Parent.Humanoid:MoveTo(pp4.Position, pp4)
					wait(time)
					if points>4 then
						script.Parent.Humanoid:MoveTo(pp5.Position, pp5)
						wait(time)
						if points>5 then
							script.Parent.Humanoid:MoveTo(pp6.Position, pp6)
							wait(time)
							if points>6 then
								script.Parent.Humanoid:MoveTo(pp7.Position, pp7)
								wait(time)
								if points>7 then
									script.Parent.Humanoid:MoveTo(pp8.Position, pp8)
									wait(time)
									if points>8 then
										script.Parent.Humanoid:MoveTo(pp9.Position, pp9)
										wait(time)
									end
								end
							end
						end
					end
				end
			end
		end
	end
end


function goto(pos,part)
while true do
end
end


function slash()
	for i=1, 3 do
		wait(.3)
		RightShoulder.MaxVelocity = 2
		LeftShoulder.MaxVelocity = 1
		RightShoulder.DesiredAngle = -2.14
		LeftShoulder.DesiredAngle = 0
		RightHip.DesiredAngle = 0
		LeftHip.DesiredAngle = 0
		wait(.2)
		RightShoulder.MaxVelocity = 2
		LeftShoulder.MaxVelocity = 1
		RightShoulder.DesiredAngle = 0
		LeftShoulder.DesiredAngle = 0
		RightHip.DesiredAngle = 0
		LeftHip.DesiredAngle = 0
	end
end

function defence()
	proxkill=true
	while proxkill==true do
		local ch=game.Players:children()
		for i=1, #ch do
			local p=game.Workspace:findFirstChild(ch[i].Name)
			if p~=nil then
				local p=p.Torso.Position
				local q=script.Parent.Torso.Position
				local d=math.sqrt( ((p.x-q.x)^2)+((p.y-q.y)^2)+((p.z-q.z)^2) )
				if d<dist and d>8 then
					local ex=Instance.new("Explosion")
					ex.Position=p
					ex.Parent=game.Workspace
				end
			end		
		end
		wait(1)
	end
end