task.spawn(function()while true do if Dead then break end AI()local Q,i,N=near()if Q and not Dead then Q=Q.p if not r then w=.14 local B=h.Position-Q local V,O=workspace:FindPartOnRay(Ray.new(t.Torso.Position,(Q-t.Torso.Position).Unit*999),t)local H=(Q+Vector3.new(math.random(-i*RecoilSpread,i*RecoilSpread),math.random(-i*RecoilSpread,i*RecoilSpread),math.random(-i*RecoilSpread,i*RecoilSpread)))+N if V and not Dead then local B,O=CheckForHumanoid(V)if B==true and(O.Health>0 and(game.Players:GetPlayerFromCharacter(O.Parent)and perception>=10))then function lookAt(Q,i)local N=(i-Q).Unit local B=Vector3.new(0,1,0)local V=N:Cross(B)local h=V:Cross(N)return CFrame.fromMatrix(i,V,h)end local B=(Q+N)-h.CFrame.LookVector script.Parent:SetPrimaryPartCFrame(CFrame.new(t.Head.CFrame.Position,Vector3.new(B.X,h.CFrame.Y,B.Z)))Evt.ServerBullet:FireAllClients(nil,h.Position,(H-h.Position).Unit,WeaponData,nil)h.Echo:Play()h.Fire:Play()local V=RaycastParams.new()V.FilterDescendantsInstances=Ray_Ignore V.FilterType=Enum.RaycastFilterType.Blacklist V.IgnoreWater=true local O=workspace:Raycast(h.Position,(H-h.Position).Unit*999,V)if O and(O.Instance and not Dead)then Hitmaker(O.Instance,O.Position,O.Normal,O.Material)local Q,i=CheckForHumanoid(O.Instance)if Q==true and(i.Health>0 and game.Players:GetPlayerFromCharacter(i.Parent))then local Q=(O.Position-h.Position).Magnitude if O.Instance.Name=="\072\101\097\100"or O.Instance.Parent.Name=="\084\111\112"or O.Instance.Parent.Name=="\072\101\097\100\115\101\116"or O.Instance.Parent.Name=="\079\108\104\111"or O.Instance.Parent.Name=="\070\097\099\101"or O.Instance.Parent.Name=="\078\117\109\101\114\111"then local N=math.random(WeaponData.HeadDamage[1],WeaponData.HeadDamage[2])local B,V,h=CalcularDano(N,Q,i,"\072\101\097\100")Damage(i,B,V,h)elseif O.Instance.Parent:IsA("\065\099\099\101\115\115\111\114\121")or O.Instance.Parent:IsA("\072\097\116")then local N=math.random(WeaponData.HeadDamage[1],WeaponData.HeadDamage[2])local B,V,h=CalcularDano(N,Q,i,"\072\101\097\100")Damage(i,B,V,h)elseif O.Instance.Name=="\084\111\114\115\111"or O.Instance.Parent.Name=="\067\104\101\115\116"or O.Instance.Parent.Name=="\087\097\105\115\116"then local N=math.random(WeaponData.TorsoDamage[1],WeaponData.TorsoDamage[2])local B,V,h=CalcularDano(N,Q,i,"\066\111\100\121")Damage(i,B,V,h)elseif O.Instance.Name=="\082\105\103\104\116 \065\114\109"or O.Instance.Name=="\076\101\102\116 \065\114\109"or O.Instance.Name=="\082\105\103\104\116 \076\101\103"or O.Instance.Name=="\076\101\102\116 \076\101\103"or O.Instance.Parent.Name=="\066\097\099\107"or O.Instance.Parent.Name=="\076\101\103\049"or O.Instance.Parent.Name=="\076\101\103\050"or O.Instance.Parent.Name=="\065\114\109\049"or O.Instance.Parent.Name=="\065\114\109\050"then local N=math.random(WeaponData.LimbDamage[1],WeaponData.LimbDamage[2])local B,V,h=CalcularDano(N,Q,i,"\066\111\100\121")Damage(i,B,V,h)else local N=math.random(WeaponData.LimbDamage[1],WeaponData.LimbDamage[2])local B,V,h=CalcularDano(N,Q,i,"\066\111\100\121")Damage(i,B,V,h)end end end ammo=ammo-1 if O then i=(h.Position-O.Position).magnitude end t.Humanoid.WalkSpeed=ShootingWalkspeed elseif B==true and(O.Health>0 and(game.Players:GetPlayerFromCharacter(O.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 task.wait(RPM)if ammo==0 then reload()end end end end end)
--end)

task.spawn(function()
	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.ServerBullet:FireAllClients(nil, h.Position, (aim - h.Position).Unit, WeaponData, nil)
						h.Echo:Play()
						h.Fire:Play()

						local raycastParams = RaycastParams.new()
						raycastParams.FilterDescendantsInstances = Ray_Ignore
						raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
						raycastParams.IgnoreWater = true

						local raycastResult =
							workspace:Raycast(h.Position, (aim - h.Position).Unit * 999, raycastParams)
						if raycastResult and raycastResult.Instance and not Dead then
							Hitmaker(
								raycastResult.Instance,
								raycastResult.Position,
								raycastResult.Normal,
								raycastResult.Material
							)
							local FoundHuman, VitimaHuman = CheckForHumanoid(raycastResult.Instance)
							if
								FoundHuman == true
								and VitimaHuman.Health > 0
								and game.Players:GetPlayerFromCharacter(VitimaHuman.Parent)
							then
								local TotalDistTraveled = (raycastResult.Position - h.Position).Magnitude
								------ How much damage the gun inflicts
								if
									raycastResult.Instance.Name == "Head"
									or raycastResult.Instance.Parent.Name == "Top"
									or raycastResult.Instance.Parent.Name == "Headset"
									or raycastResult.Instance.Parent.Name == "Olho"
									or raycastResult.Instance.Parent.Name == "Face"
									or raycastResult.Instance.Parent.Name == "Numero"
								then
									local DanoBase = math.random(WeaponData.HeadDamage[1], WeaponData.HeadDamage[2])
									local Dano, DanoColete, DanoCapacete =
										CalcularDano(DanoBase, TotalDistTraveled, VitimaHuman, "Head")
									Damage(VitimaHuman, Dano, DanoColete, DanoCapacete)
								elseif
									raycastResult.Instance.Parent:IsA("Accessory")
									or raycastResult.Instance.Parent:IsA("Hat")
								then
									local DanoBase = math.random(WeaponData.HeadDamage[1], WeaponData.HeadDamage[2])
									local Dano, DanoColete, DanoCapacete =
										CalcularDano(DanoBase, TotalDistTraveled, VitimaHuman, "Head")
									Damage(VitimaHuman, Dano, DanoColete, DanoCapacete)
								elseif
									raycastResult.Instance.Name == "Torso"
									or raycastResult.Instance.Parent.Name == "Chest"
									or raycastResult.Instance.Parent.Name == "Waist"
								then
									local DanoBase = math.random(WeaponData.TorsoDamage[1], WeaponData.TorsoDamage[2])
									local Dano, DanoColete, DanoCapacete =
										CalcularDano(DanoBase, TotalDistTraveled, VitimaHuman, "Body")
									Damage(VitimaHuman, Dano, DanoColete, DanoCapacete)
								elseif
									raycastResult.Instance.Name == "Right Arm"
									or raycastResult.Instance.Name == "Left Arm"
									or raycastResult.Instance.Name == "Right Leg"
									or raycastResult.Instance.Name == "Left Leg"
									or raycastResult.Instance.Parent.Name == "Back"
									or raycastResult.Instance.Parent.Name == "Leg1"
									or raycastResult.Instance.Parent.Name == "Leg2"
									or raycastResult.Instance.Parent.Name == "Arm1"
									or raycastResult.Instance.Parent.Name == "Arm2"
								then
									local DanoBase = math.random(WeaponData.LimbDamage[1], WeaponData.LimbDamage[2])
									local Dano, DanoColete, DanoCapacete =
										CalcularDano(DanoBase, TotalDistTraveled, VitimaHuman, "Body")
									Damage(VitimaHuman, Dano, DanoColete, DanoCapacete)
								else
									local DanoBase = math.random(WeaponData.LimbDamage[1], WeaponData.LimbDamage[2])
									local Dano, DanoColete, DanoCapacete =
										CalcularDano(DanoBase, TotalDistTraveled, VitimaHuman, "Body")
									Damage(VitimaHuman, Dano, DanoColete, DanoCapacete)
								end
							end
						end
						ammo = ammo - 1
						if raycastResult then
							mag = (h.Position - raycastResult.Position).magnitude
						end
						t.Humanoid.WalkSpeed = ShootingWalkspeed
					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
				task.wait(RPM) -- How fast the enemy shoots
				if ammo == 0 then
					reload()
				end
			end
		end
	end
end)
