local M local V local h local Q=0 if canWander then spawn(function()while Q==0 do Q=1 local M,V=hroot.Position.x+math.random(-WanderX,WanderX),hroot.Position.z+math.random(-WanderZ,WanderZ)human:MoveTo(Vector3.new(M,0,V))wait(math.random(4,6))Q=0 end end)end while wait()do local o=GetTorso(hroot.Position)if o~=nil then Q=1 local function d(M)local V=3 if V>#M then V=3 end if M[V]==nil and V<#M then repeat V=V+1 wait()until M[V]~=nil return Vector3.new(1,0,0)+M[V]else V=3 return M[V]end end M=pfs:FindPathAsync(hroot.Position,o.Position)V=M:GetWaypoints()h=V local c local N=Vector3.FromNormalId(Enum.NormalId.Front)local A=hroot.CFrame*CFrame.new(N)N=A.p.unit local E=Ray.new(hroot.Position,N)local a,b=game.Workspace:FindPartOnRay(E,hroot)if M and V or d(V)then if d(V)~=nil and(d(V)).Action==Enum.PathWaypointAction.Walk then human:MoveTo((d(V)).Position)human.Jump=false end if d(V)~=nil and(d(V)).Action==Enum.PathWaypointAction.Jump then human.Jump=true c=human.Changed:connect(function()human.Jump=true end)human:MoveTo((d(V)).Position)else human.Jump=false end hroot.Touched:connect(function(M)local V=GetPlayersBodyParts(o)if M:IsA("\080\097\114\116")and(not M.Name==V and(a and(a.Name~=V and(a:IsA("\080\097\114\116")and E:Distance(a.Position)<5))))then c=human.Changed:connect(function()human.Jump=true end)else human.Jump=false end end)if c then c:Disconnect()end else for M=3,#h,1 do human:MoveTo(h[M].Position)end end elseif o==nil and canWander then Q=0 M=nil V=nil human.MoveToFinished:Wait()end end
-- no touchy
local path
local waypoint
local oldpoints
local isWandering = 0

if canWander then
	spawn(function()
		while isWandering == 0 do
			isWandering = 1
			local desgx, desgz =
				hroot.Position.x + math.random(-WanderX, WanderX), hroot.Position.z + math.random(-WanderZ, WanderZ)
			human:MoveTo(Vector3.new(desgx, 0, desgz))
			wait(math.random(4, 6))
			isWandering = 0
		end
	end)
end

while wait() do
	local enemytorso = GetTorso(hroot.Position)
	if enemytorso ~= nil then -- if player detected
		isWandering = 1
		local function checkw(t)
			local ci = 3
			if ci > #t then
				ci = 3
			end
			if t[ci] == nil and ci < #t then
				repeat
					ci = ci + 1
					wait()
				until t[ci] ~= nil
				return Vector3.new(1, 0, 0) + t[ci]
			else
				ci = 3
				return t[ci]
			end
		end

		path = pfs:FindPathAsync(hroot.Position, enemytorso.Position)
		waypoint = path:GetWaypoints()
		oldpoints = waypoint
		local connection

		local direct = Vector3.FromNormalId(Enum.NormalId.Front)
		local ncf = hroot.CFrame * CFrame.new(direct)
		direct = ncf.p.unit
		local rootr = Ray.new(hroot.Position, direct)
		local phit, ppos = game.Workspace:FindPartOnRay(rootr, hroot)

		if path and waypoint or checkw(waypoint) then
			if checkw(waypoint) ~= nil and checkw(waypoint).Action == Enum.PathWaypointAction.Walk then
				human:MoveTo(checkw(waypoint).Position)
				human.Jump = false
			end

			-- CANNOT LET BALDI JUMPS --
			if checkw(waypoint) ~= nil and checkw(waypoint).Action == Enum.PathWaypointAction.Jump then
				human.Jump = true
				connection = human.Changed:connect(function()
					human.Jump = true
				end)
				human:MoveTo(checkw(waypoint).Position)
			else
				human.Jump = false
			end

			hroot.Touched:connect(function(p)
				local bodypartnames = GetPlayersBodyParts(enemytorso)
				if
					p:IsA("Part")
					and not p.Name == bodypartnames
					and phit
					and phit.Name ~= bodypartnames
					and phit:IsA("Part")
					and rootr:Distance(phit.Position) < 5
				then
					connection = human.Changed:connect(function()
						human.Jump = true
					end)
				else
					human.Jump = false
				end
			end)

			if connection then
				connection:Disconnect()
			end
		else
			for i = 3, #oldpoints do
				human:MoveTo(oldpoints[i].Position)
			end
		end
	elseif enemytorso == nil and canWander then -- if player not detected
		isWandering = 0
		path = nil
		waypoint = nil
		human.MoveToFinished:Wait()
	end
end
