Is this bug in my code or roblox pathfinding bug?

once i remove the wall my npc acts fine but if i add it back he bugs out
https://streamable.com/q97elq
what can cause this issue and how to fix it?

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) or ray.Instance.Transparency < 1 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
1 Like

i fixed it by making doors bigger

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.