while true do if Dead then break end AI()local i,j,x=near()if i and not Dead then i=i.p if not r then w=.14 local r=h.Position-i local E,H=workspace:FindPartOnRay(Ray.new(t.Torso.Position,(i-t.Torso.Position).Unit*999),t)local O=(i+Vector3.new(math.random(-j*RecoilSpread,j*RecoilSpread),math.random(-j*RecoilSpread,j*RecoilSpread),math.random(-j*RecoilSpread,j*RecoilSpread)))+x if E and not Dead then local r,H=CheckForHumanoid(E)if r==true and(H.Health>0 and(game.Players:GetPlayerFromCharacter(H.Parent)and perception>=10))then function lookAt(r,i)local j=(i-r).Unit local t=Vector3.new(0,1,0)local x=j:Cross(t)local E=x:Cross(j)return CFrame.fromMatrix(i,x,E)end local r=(i+x)-h.CFrame.LookVector script.Parent:SetPrimaryPartCFrame(CFrame.new(t.Head.CFrame.Position,Vector3.new(r.X,h.CFrame.Y,r.Z)))Evt.ACS_AI.AIShoot:FireAllClients(h)h.Echo:Play()h.Fire:Play()local E,H,n,C=workspace:FindPartOnRay(Ray.new(h.Position,(O-h.Position).Unit*999),t)if E and not Dead then Hitmaker(E,H,n,C)local r,i=CheckForHumanoid(E)if r==true and(i.Health>0 and game.Players:GetPlayerFromCharacter(i.Parent))then local r=(H-h.Position).Magnitude if E.Name=="\072\101\097\100"or E.Parent.Name=="\084\111\112"or E.Parent.Name=="\072\101\097\100\115\101\116"or E.Parent.Name=="\079\108\104\111"or E.Parent.Name=="\070\097\099\101"or E.Parent.Name=="\078\117\109\101\114\111"then local j=math.random(HeadDamage[1],HeadDamage[2])local t,x,E=CalcularDano(j,r,i,"\072\101\097\100")Damage(i,t,x,E)elseif E.Parent:IsA("\065\099\099\101\115\115\111\114\121")or E.Parent:IsA("\072\097\116")then local j=math.random(HeadDamage[1],HeadDamage[2])local t,x,E=CalcularDano(j,r,i,"\072\101\097\100")Damage(i,t,x,E)elseif E.Name=="\084\111\114\115\111"or E.Parent.Name=="\067\104\101\115\116"or E.Parent.Name=="\087\097\105\115\116"then local j=math.random(TorsoDamage[1],TorsoDamage[2])local t,x,E=CalcularDano(j,r,i,"\066\111\100\121")Damage(i,t,x,E)elseif E.Name=="\082\105\103\104\116 \065\114\109"or E.Name=="\076\101\102\116 \065\114\109"or E.Name=="\082\105\103\104\116 \076\101\103"or E.Name=="\076\101\102\116 \076\101\103"or E.Parent.Name=="\066\097\099\107"or E.Parent.Name=="\076\101\103\049"or E.Parent.Name=="\076\101\103\050"or E.Parent.Name=="\065\114\109\049"or E.Parent.Name=="\065\114\109\050"then local j=math.random(LimbsDamage[1],LimbsDamage[2])local t,x,E=CalcularDano(j,r,i,"\066\111\100\121")Damage(i,t,x,E)else local j=math.random(LimbsDamage[1],LimbsDamage[2])local t,x,E=CalcularDano(j,r,i,"\066\111\100\121")Damage(i,t,x,E)end end end ammo=ammo-1 j=(h.Position-H).magnitude t.Humanoid.WalkSpeed=ShootingWalkspeed local R=math.random(1,100)if R<=Settings.TracerChance.Value then Evt.ACS_AI.AIBullet:FireAllClients(nil,t.Head.CFrame,Tracer,0,2200,(O-h.Position).Unit,TracerColor,Ray_Ignore,BulletFlare,BulletFlareColor)end elseif r==true and(H.Health>0 and(game.Players:GetPlayerFromCharacter(H.Parent)and perception<10))then perception=perception+Settings.Level.Value t.Humanoid.WalkSpeed=SearchingWalkspeed Memory=Settings.Level.Value*100 CanSee=true elseif perception>0 then perception=perception-(1/Settings.Level.Value)*2 t.Humanoid.WalkSpeed=SearchingWalkspeed CanSee=false elseif perception<=0 then perception=0 t.Humanoid.WalkSpeed=RegularWalkspeed Memory=Memory-.25 end end wait(RPM)if ammo==0 then reload()end end end end
--end)

while true do
	if Dead then
		break
	end

	AI()

	local n, mag, hum = near()
	if n and not Dead then
		n = n.p
		if not r then
			w = 0.14
			local dir = h.Position - n
			local tst, _ = workspace:FindPartOnRay(Ray.new(t.Torso.Position, (n - t.Torso.Position).Unit * 999), t)
			local aim = n
				+ Vector3.new(
					math.random(-mag * RecoilSpread, mag * RecoilSpread),
					math.random(-mag * RecoilSpread, mag * RecoilSpread),
					math.random(-mag * RecoilSpread, mag * RecoilSpread)
				)
				+ hum

			if tst and not Dead then
				local FoundHuman, VitimaHuman = CheckForHumanoid(tst)
				if
					FoundHuman == true
					and VitimaHuman.Health > 0
					and game.Players:GetPlayerFromCharacter(VitimaHuman.Parent)
					and perception >= 10
				then
					function lookAt(target, eye)
						local forwardVector = (eye - target).Unit
						local upVector = Vector3.new(0, 1, 0)
						-- You have to remember the right hand rule or google search to get this right
						local rightVector = forwardVector:Cross(upVector)
						local upVector2 = rightVector:Cross(forwardVector)

						return CFrame.fromMatrix(eye, rightVector, upVector2)
					end

					local direction = (n + hum) - h.CFrame.LookVector
					script.Parent:SetPrimaryPartCFrame(
						CFrame.new(t.Head.CFrame.Position, Vector3.new(direction.X, h.CFrame.Y, direction.Z))
					)

					Evt.ACS_AI.AIShoot:FireAllClients(h)
					h.Echo:Play()
					h.Fire:Play()
					local par, pos, Norm, Mat =
						workspace:FindPartOnRay(Ray.new(h.Position, (aim - h.Position).Unit * 999), t)
					if par and not Dead then
						Hitmaker(par, pos, Norm, Mat)
						local FoundHuman, VitimaHuman = CheckForHumanoid(par)
						if
							FoundHuman == true
							and VitimaHuman.Health > 0
							and game.Players:GetPlayerFromCharacter(VitimaHuman.Parent)
						then
							local TotalDistTraveled = (pos - h.Position).Magnitude
							------ How much damage the gun inflicts
							if
								par.Name == "Head"
								or par.Parent.Name == "Top"
								or par.Parent.Name == "Headset"
								or par.Parent.Name == "Olho"
								or par.Parent.Name == "Face"
								or par.Parent.Name == "Numero"
							then
								local DanoBase = math.random(HeadDamage[1], HeadDamage[2])
								local Dano, DanoColete, DanoCapacete =
									CalcularDano(DanoBase, TotalDistTraveled, VitimaHuman, "Head")
								Damage(VitimaHuman, Dano, DanoColete, DanoCapacete)
							elseif par.Parent:IsA("Accessory") or par.Parent:IsA("Hat") then
								local DanoBase = math.random(HeadDamage[1], HeadDamage[2])
								local Dano, DanoColete, DanoCapacete =
									CalcularDano(DanoBase, TotalDistTraveled, VitimaHuman, "Head")
								Damage(VitimaHuman, Dano, DanoColete, DanoCapacete)
							elseif par.Name == "Torso" or par.Parent.Name == "Chest" or par.Parent.Name == "Waist" then
								local DanoBase = math.random(TorsoDamage[1], TorsoDamage[2])
								local Dano, DanoColete, DanoCapacete =
									CalcularDano(DanoBase, TotalDistTraveled, VitimaHuman, "Body")
								Damage(VitimaHuman, Dano, DanoColete, DanoCapacete)
							elseif
								par.Name == "Right Arm"
								or par.Name == "Left Arm"
								or par.Name == "Right Leg"
								or par.Name == "Left Leg"
								or par.Parent.Name == "Back"
								or par.Parent.Name == "Leg1"
								or par.Parent.Name == "Leg2"
								or par.Parent.Name == "Arm1"
								or par.Parent.Name == "Arm2"
							then
								local DanoBase = math.random(LimbsDamage[1], LimbsDamage[2])
								local Dano, DanoColete, DanoCapacete =
									CalcularDano(DanoBase, TotalDistTraveled, VitimaHuman, "Body")
								Damage(VitimaHuman, Dano, DanoColete, DanoCapacete)
							else
								local DanoBase = math.random(LimbsDamage[1], LimbsDamage[2])
								local Dano, DanoColete, DanoCapacete =
									CalcularDano(DanoBase, TotalDistTraveled, VitimaHuman, "Body")
								Damage(VitimaHuman, Dano, DanoColete, DanoCapacete)
							end
						end
					end
					ammo = ammo - 1
					mag = (h.Position - pos).magnitude
					t.Humanoid.WalkSpeed = ShootingWalkspeed
					--print("Atirando")

					local TracerChance = math.random(1, 100)

					if TracerChance <= Settings.TracerChance.Value then
						Evt.ACS_AI.AIBullet:FireAllClients(
							nil,
							t.Head.CFrame,
							Tracer,
							0,
							2200,
							(aim - h.Position).Unit,
							TracerColor,
							Ray_Ignore,
							BulletFlare,
							BulletFlareColor
						)
					end
				elseif
					FoundHuman == true
					and VitimaHuman.Health > 0
					and game.Players:GetPlayerFromCharacter(VitimaHuman.Parent)
					and perception < 10
				then
					perception = perception + Settings.Level.Value
					t.Humanoid.WalkSpeed = SearchingWalkspeed
					Memory = Settings.Level.Value * 100
					CanSee = true
				elseif perception > 0 then
					perception = perception - 1 / Settings.Level.Value * 2
					t.Humanoid.WalkSpeed = SearchingWalkspeed
					CanSee = false
				elseif perception <= 0 then
					perception = 0
					t.Humanoid.WalkSpeed = RegularWalkspeed
					Memory = Memory - 0.25
				end
				--print(Memory)
			end
			wait(RPM) -- How fast the enemy shoots
			if ammo == 0 then
				reload()
			end
		end
	end
end
