When AI goes into the office he just breaks

npc acts fine until he goes into the office
https://streamable.com/9e743p

	wait(game.Loaded)

	local PathfindingService = game:GetService("PathfindingService")

	local Bobby = script.Parent
	local humanoid = Bobby:WaitForChild("Humanoid")
	local Humanoid_Root_Part = Bobby:WaitForChild("HumanoidRootPart")

	Humanoid_Root_Part:SetNetworkOwner(nil)

	local pathParams = {
		AgentHeight = 5,
		AgentRadius = 3,
		AgentCanJump = true,
	}

	local rayParams = RaycastParams.new()
	rayParams.FilterType = Enum.RaycastFilterType.Exclude
	rayParams.FilterDescendantsInstances = {Bobby}

	local lastPos
	local RANGE = 150
	local DAMAGE = 100

	local function canSeeTarget(target)
		local orgin = Humanoid_Root_Part.Position
		local direction = (target.HumanoidRootPart.Position - Humanoid_Root_Part.Position).Unit * RANGE
		local ray = workspace:Raycast(orgin, direction, rayParams)
		if ray then
		if ray.Instance:IsDescendantOf(target) then
			print("Bobby Saw Target")
				return true
			else
				return false
			end
		else
			return false
		end
	end
	local function findTarget()
		local players = game.Players:GetPlayers()
		local maxDistance = RANGE
		local nearestTarget

		for i, player in pairs(players) do
			if player.Character then
				local target = player.Character
				local distance = (Humanoid_Root_Part.Position - target.HumanoidRootPart.Position).Magnitude
				
				if distance < maxDistance and canSeeTarget(target) or distance < 20  then
					nearestTarget = target
					maxDistance = distance
					humanoid.WalkSpeed = 50
					Bobby.HumanoidRootPart.Walking.Playing = false
				Bobby.HumanoidRootPart.Running.Playing = true
				print("Target Found")
				else
					humanoid.WalkSpeed = 6
					Bobby.HumanoidRootPart.Running.Playing = false
					Bobby.HumanoidRootPart.Walking.Playing = true
				end
			end
		end

		return nearestTarget
	end

	local function getPath(destination)
		local path = PathfindingService:CreatePath(pathParams)

		path:ComputeAsync(Humanoid_Root_Part.Position, destination.Position)
print("bobby getting waypoint Paths")
		return path	
	end

	local function attack(target)
		local distance = (Humanoid_Root_Part.Position - target.HumanoidRootPart.Position).Magnitude
		local debounce = false
	 local player = game.Players:GetPlayerFromCharacter(target)
		if distance > 5 then
			humanoid:MoveTo(target.HumanoidRootPart.Position)
		else
		if debounce == false then
			print("boby killing player")
				debounce = true

				target.Humanoid.Health -= DAMAGE
				Bobby.Head.JumpScare:Play()
				game.ReplicatedStorage.Remotes.DeathEvent:FireClient(player)
				task.wait()
				debounce = false
			end
		end
	end

	local function walkTo(destination)
		local path = getPath(destination)

		if path.Status == Enum.PathStatus.Success then
			for i, waypoint in pairs(path:GetWaypoints()) do
				path.Blocked:Connect(function()
				path:Destroy()
				print("path is blocked")
				getPath(destination)
				end)

				local target = findTarget()

				if target and target.Humanoid.Health > 0 then
					lastPos = target.HumanoidRootPart.Position
					attack(target)
					break
				else
					if waypoint.Action == Enum.PathWaypointAction.Jump then
						humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
					end
					if lastPos then
						humanoid:MoveTo(lastPos)
						humanoid.WalkSpeed = 50
						humanoid.MoveToFinished:Wait()
						lastPos = nil
						humanoid.WalkSpeed = 6
					print("player wasnt found ):")
						break
					else
						humanoid:MoveTo(waypoint.Position)
							humanoid.MoveToFinished:Wait()
							end
					end
				end
		else
			return
		end
	end

	local function patrol()
		local waypoints = workspace.waypoints:GetChildren()
		local randomNum = math.random(1, #waypoints)
		walkTo(waypoints[randomNum])
		end

	while task.wait(0.2) do
		patrol()
	end
4 Likes

The only thing I can think of is the timeout on “MoveToFinished”. Specifically when you’re waiting on it at the end. You have to continue calling the MoveTo before the MoveToFinished to give it more time to catch the player. I’m a little rusty, so forgive me if I overlooked something, but I feel like I’ve a similar issue before. It’s also roughly 8 seconds from the NPC’s initial engagement with the player to when it freezes, which is why I thought of that. Hope this could be of some help.

when i’m out of office he will keep chasing me so i don’t think its MoveToFinished Issue

I just noticed the output in the bottom corner. It seems when the NPC hangs up there is a spam of “bobby getting waypoint Paths” in the output. Looks like that is being printed when the event is fired from when the path is obstructed? Try checking how you reassess the path after it’s been broken. (Edit: looks like when the path is blocked and the loop continues, it’s trying to get too many waypoints too quickly)

1 Like

yea but it happens only when i enter the office

1 Like

Is this the only place is occurs at in the entire game?

1 Like

yes it is, i don’t know why its like that

1 Like

he can catch the player only if player is outside the office even if its on another side
but if player is in the office he just breaks https://streamable.com/krdbqz

Are there any unions or can’t collide parts in the office that could be interfering with the AI? I’m thinking it has to be something with the path blocking function. If it only happens in the office there’s a good chance it’s building interference.

i already fixed it thanks i just made the doors bigger

Yeah. For the future, two walls that are too close together can count as a wall to the pathfinding AI. So for instance, the sides of your doorway, the AI was seeing as a wall but still trying to follow you and that’s why your blocked function was firing. I assume it started working again when you left the room because it established that there is a path out? I’ve heard of the parts being too close before but not of it fixing itself. Interesting bug though glad you got it worked out.