Pathfinding monster trying to go between waypoints and target player

ahhh i feel like im going crazy trying to get this monster NPC script together and working the way i want.

the script i have made so far works, but it seems like the monster is trying to go between moving to a waypoint and moving to the target player. it walks towards the target, then turns around for a second like its going to a different waypoint, then turns back around and goes for the target again.

it also does a little circle when trying to maneuver around corners. ive made sure i set the HipHeight correctly, and even tweaked it a little to see if that was the problem. that didnt work.

do you guys see anything wrong with this script that im just over looking?
ive been working on it so long that i feel like im looking at parts that may not be the problem. I need a fresh set of eyes to take a look lol

local NPC = script.Parent
local npcRoot = NPC:WaitForChild(“HumanoidRootPart”)
local npcHumanoid = NPC:WaitForChild(“Humanoid”)
local Players = game:GetService(“Players”)
local PathfindingService = game:GetService(“PathfindingService”)

NPC.PrimaryPart:SetNetworkOwner(nil)

task.wait(3)

local function findNearestPlayer()
local nearestPlayer = nil
local shortestDistance = 100

for i, player in pairs(Players:GetPlayers()) do
	if player.Character and player.Character:WaitForChild("HumanoidRootPart") then
		local distance = (npcRoot.Position - player.Character.HumanoidRootPart.Position).magnitude
		if distance < shortestDistance then
			-- Perform a raycast to check line of sight
			local origin = npcRoot.Position
			local direction = (player.Character.HumanoidRootPart.Position - origin).unit * distance
			local raycastParams = RaycastParams.new()
			raycastParams.FilterDescendantsInstances = {player.Character}
			raycastParams.FilterType = Enum.RaycastFilterType.Include

			local raycastResult = workspace:Raycast(origin, direction, raycastParams)
			if raycastResult and raycastResult.Instance:IsDescendantOf(player.Character) then
				shortestDistance = distance
				nearestPlayer = player
			else
				print("no target")
			end
		end
	end
end
return nearestPlayer

end

local function moveToTarget(target)
local path = PathfindingService:CreatePath({
AgentRadius = 10,
AgentHeight = 10,
AgentCanJump = false,

	Costs = {
		Water = math.huge,
		DangerZone = math.huge
	}
})

path:ComputeAsync(NPC.PrimaryPart.Position, target.Character.HumanoidRootPart.Position)

if path.Status == Enum.PathStatus.Success then       ------------------ i think the problem is here
	for i, waypoint in pairs(path:GetWaypoints()) do
		npcHumanoid:MoveTo(waypoint.Position)
		npcHumanoid.MoveToFinished:Wait(1)       ------------------
	end
end
return path

end

while wait(0.1) do
local target = findNearestPlayer()
if target and target.Character and target.Character:FindFirstChild(“HumanoidRootPart”) then
print(target.Name)
moveToTarget(target)
end
end

I forgot to mention that its moving to the players last position instead of directly towards them. so if its walking towards me and i move, it will go to the position i was just at before pathing to my current position.

Try deleting the line

npcHumanoid.MoveToFinished:Wait(1)

This way, the monsters won’t have a delay and will move directly towards the player

the monster moves directly to the player now which is awesome! the only thing is that it now can get stuck on walls instead of moving around them.

Why are you looping from all the waypoints? that is not how to do a pathfinding script. it makes it less dynamic and moving walls and objects can cause it to get stuck

That was what some of the tutorial vids were showing so I just went along with it. I can script most things just fine but for some reason the pathfinding just stumps me.

What I did was just used GnomeCode’s teddy ai and tweaked the behaviors to what I want.

I also found these posts that may solve the monster spinning and sliding;

2 Likes

Don’t actually remove this line lol

npcHumanoid.MoveToFinished:Wait()

That’s just making your monster walk to the very last waypoint it’s as if you are doing

npcHumanoid:MoveTo(target.Character.HumanoidRootPart.Position)

This will make your whole point of a pathfinding pointless.

Another thing to note the :Wait(), the number inside the brackets doesn’t do anything.

I also noticed you use filter type include. This will point a ray at the player but whether or not there’s anything to intercept is nil meaning the only part that the ray will touch is the player so the monster will always see the player.

Anyways about your problem might be the size of the monster. Try changing the AgentRadius value and see if that helps, I believe the size of the monster is the problem since he’s probably too big that the next waypoint after moving is actually now behind the monster which is why he turned around and back.

1 Like

Ohh okay that makes sense! I really appreciate you taking the time to explain :smiley:

I did mess around with the agent radius but what ended up being the problem there was the scale of the model. I imported them from blender and downsized in studio so the scale was all out of wack lol

1 Like