My NPC isn't pathfinding how I want it to

Hey there!

I am trying to get an NPC to pathfind in my direction, but I’m having a couple of problems;
1: The NPC will not jump to try and get to a player.
2: The NPC will not go around obstacles, and will instead just faceplant them.
(Here is a video breaking it down visually: https://streamable.com/vpx849)

Here is the very confusing code I am currently using (it’s my first time using pathfinding):

local allowedMove = true
local nextWaypointIndex = 2
local reachedConnection
local blockedConnection

local hasTarget = false
local currentTarget

local function FollowPlayer()
	local pathParams = {
		["AgentHeight"] = 5,
		["AgentRadius"] = 4,
		["AgentCanJump"] = true
	}
	local path
	
	if allowedMove and not hasTarget then
		path = pFS:CreatePath(pathParams)
		local movingToPosition = character.HumanoidRootPart.Position - Vector3.new(math.random(-5,5), 0, math.random(-5,5))
		
		local success, errorMessage = pcall(function()
			local ComputedPath = path:ComputeAsync(monkeyNoid.RootPart.Position, movingToPosition)
		end)

		if success and path.Status == Enum.PathStatus.Success then
		else
			if errorMessage ~= nil then
				print(errorMessage)
			end
		end
	elseif hasTarget then
		path = pFS:CreatePath(pathParams)
		local movingToPosition = currentTarget.HumanoidRootPart.Position - Vector3.new(math.random(-5,5), 0, math.random(-5,5))
		
		local success, errorMessage = pcall(function()
			local ComputedPath = path:ComputeAsync(monkeyNoid.RootPart.Position, movingToPosition)
		end)

		if success and path.Status == Enum.PathStatus.Success then
		else
			if errorMessage ~= nil then
				print(errorMessage)
			end
		end
	else
		path = nil
	end
	
	return path
end

runS.Stepped:Connect(function()
	if (monkeyNoid.RootPart.Position - character.HumanoidRootPart.Position).Magnitude < 5 then
		allowedMove = false
	else
		allowedMove = true
		
		local path = FollowPlayer()
		
		if path then
			local waypoints = path:GetWaypoints()
			for _, waypoint in pairs(waypoints) do
				local part = Instance.new("Part", workspace)
				part.Shape = Enum.PartType.Ball
				part.BrickColor = BrickColor.White()
				part.Material = Enum.Material.Neon
				part.CanCollide = false
				part.Anchored = true
				part.Size = Vector3.new(.5,.5,.5)
				part.Locked = true
				part.Position = waypoint.Position
				game:GetService("Debris"):AddItem(part, 1)
				
				monkeyNoid:MoveTo(waypoint.Position)
			end
		end
	end
end)

enemyMarkerEvent.OnServerEvent:Connect(function(player, target)
	if player == players:GetPlayerFromCharacter(character) then
		
		if target then
			currentTarget = target
			allowedMove = false
			hasTarget = true
		else
			hasTarget = false
			currentTarget = nil
		end
	end
end)

Any help is greatly appreciated! Hopefully you kept all your sanity reading through that code…

1 Like

It seems like you are creating a new path every frame therefore new paths cannot really run (old paths are not finished). Therefore the NPC won’t really work the way you want it to. I suggest using simplepath (SimplePath - Pathfinding Module) if you’re new to pathfinding.

2 Likes

In addition, try to disable the parts as it may interfere with pathfinding.

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