--//=================================\\
--|| 			WRAP
--\\=================================//


function BEGINAI()
	HUMANOID.Died:Connect(function()
		DEAD = true
		script:Remove()
	end)
	coroutine.resume(coroutine.create(function()
		repeat
			wait()
			local TARGETVARIANTS = {}
			local PICK = nil
			for index, CHILD in pairs(workspace:GetDescendants()) do
				local HUMAN = CHILD:FindFirstChildOfClass("Humanoid")
				if HUMAN and CHILD:FindFirstChild("MinecraftHerobrineMainScript") == nil then
					if HUMAN.Health ~= 0 then
						if HUMAN.Torso then
							local DISTANCE = (HUMAN.Torso.Position - TORSO.Position).Magnitude
							table.insert(TARGETVARIANTS,{CHILD,DISTANCE})
						end
					end
				end
			end
			local DIST = math.huge
			for E = 1, #TARGETVARIANTS do
				local PLAYER = TARGETVARIANTS[E]
				local CHARACTER = PLAYER[1]
				local DISTANCE = PLAYER[2]
				if DISTANCE < DIST then
					DIST = DISTANCE
					PICK = CHARACTER
				end
			end
			TARGET = PICK
			if TURNTO then
				GYRO.CFrame = CF(FAKEHEAD.Position,TURNTO.Position+VT(0,TURNTO.Size.Y/1.5,0))
			else
				GYRO.CFrame = HEAD.CFrame
			end
		until DEAD == true
	end))
	while true do
		if DEAD == true then
			break
		end
		if TARGET then
			local HUM = TARGET:FindFirstChildOfClass("Humanoid")
			if HUM then
				local ROOT = HUM.Torso
				if ROOT then
					local PATHFIND = game:GetService("PathfindingService"):ComputeRawPathAsync(TORSO.Position,ROOT.Position,2)
					if PATHFIND then
						local FOE = TARGET
						TURNTO = ROOT
						local PS = PATHFIND:GetPointCoordinates()
						local PREV = TORSO.Position
						local BR = #PS > 1 and 2 or 1
						for i = BR,#PS do
							local POINT = PS[i]
							local DIST = math.huge
							repeat wait(0.1)
								DIST = (TORSO.Position - POINT).Magnitude
								HUMANOID:MoveTo(POINT)
								if (ROOT.Position - TORSO.Position).Magnitude < 7 then
									StabAnim()
									ApplyDamage(HUM,HUM.MaxHealth/20)
									local HIT = IT("Sound",ROOT)
									HIT.SoundId = "rbxassetid://"..HITSOUNDS[MRANDOM(1,#HITSOUNDS)]
									HIT.Parent = ROOT
									HIT.Pitch = 1
									HIT.Volume = 1.5
									HIT:Play()
									Debris:AddItem(HIT,2)
									if HUM.Health == 0 then
										break
									end
									wait(0.4)
								end
							until DIST < 5 or TARGET ~= FOE
							if TARGET ~= FOE then
								break
							end
						end
					else
						TURNTO = nil
					end
				else
					TURNTO = nil
				end
			else
				TURNTO = nil
			end
		else
			TURNTO = nil
		end
		wait()
	end
end

BEGINAI()
