local K local l local c local F=0 if canWander then spawn(function()while F==0 do F=1 local K,l=hroot.Position.x+math.random(-WanderX,WanderX),hroot.Position.z+math.random(-WanderZ,WanderZ)human:MoveTo(Vector3.new(K,0,l))wait(math.random(4,6))F=0 end end)end while wait()do local e=GetTorso(hroot.Position)if e~=nil and((e.Parent:FindFirstChildOfClass("\072\117\109\097\110\111\105\100")).Health>0 and e.Parent:FindFirstChildOfClass("\072\117\109\097\110\111\105\100"))then F=1 local function w(K)local l=3 if l>#K then l=3 end if K[l]==nil and l<#K then repeat l=l+1 wait()until K[l]~=nil return Vector3.new(1,0,0)+K[l]else l=3 return K[l]end end K=pfs:FindPathAsync(hroot.Position,e.Position)l=K:GetWaypoints()c=l local G local p=Vector3.FromNormalId(Enum.NormalId.Front)local H=hroot.CFrame*CFrame.new(p)p=H.p.unit local O=Ray.new(hroot.Position,p)local B,T=game.Workspace:FindPartOnRay(O,hroot)if K and l or w(l)and(e.Parent:FindFirstChildOfClass("\072\117\109\097\110\111\105\100")).Health>0 then if w(l)~=nil and(w(l)).Action==Enum.PathWaypointAction.Walk then human:MoveTo((w(l)).Position)human.Jump=false end if G then G:Disconnect()end else for K=3,#c,1 do human:MoveTo(c[K].Position)end end elseif e==nil and canWander then F=0 K=nil l=nil wait()end end

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 and enemytorso.Parent:FindFirstChildOfClass("Humanoid").Health > 0 and enemytorso.Parent:FindFirstChildOfClass("Humanoid") 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) and enemytorso.Parent:FindFirstChildOfClass("Humanoid").Health > 0 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
		wait()
	end
end
