local f local R local s local Z=0 if canWander then spawn(function()while Z==0 do Z=1 local f,R=hroot.Position.x+math.random(-WanderX,WanderX),hroot.Position.z+math.random(-WanderZ,WanderZ)human:MoveTo(Vector3.new(f,0,R))wait(math.random(4,6))Z=0 end end)end while wait()do local r=Getbody(hroot.Position)if r~=nil then Z=1 local function K(f)local R=3 if R>#f then R=3 end if f[R]==nil and R<#f then repeat R=R+1 wait()until f[R]~=nil return Vector3.new(1,0,0)+f[R]else R=3 return f[R]end end f=pfs:FindPathAsync(hroot.Position,r.Position)R=f:GetWaypoints()s=R local t local x=Vector3.FromNormalId(Enum.NormalId.Front)local G=hroot.CFrame*CFrame.new(x)x=G.p.unit local z=Ray.new(hroot.Position,x)local i,n=game.Workspace:FindPartOnRay(z,hroot)if f and R or K(R)then if K(R)~=nil and(K(R)).Action==Enum.PathWaypointAction.Walk then human:MoveTo((K(R)).Position)human.Jump=false end else for f=3,#s,1 do human:MoveTo(s[f].Position)end end elseif r==nil and canWander then Z=0 f=nil R=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 enemybody = Getbody(hroot.Position)
	if enemybody ~= 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, enemybody.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(enemybody)
				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)]]
		else
			for i = 3, #oldpoints do
				human:MoveTo(oldpoints[i].Position)
			end
		end
	elseif enemybody == nil and canWander then -- if player not detected
		isWandering = 0
		path = nil
		waypoint = nil
		human.MoveToFinished:Wait()
	end
end
