NPC makes stops while chasing target

Hello everyone, I’m having this issue when npc is chasing target it only occurs when distance is less than 8

Here is the code:

local humanoid = ugly.Humanoid
	
local PathfindingService = game:GetService("PathfindingService")
ugly.PrimaryPart:SetNetworkOwner(nil)

local function canSeeTarget(target)
	local origin = ugly.HumanoidRootPart.Position
	local direction = (target.HumanoidRootPart.Position - ugly.HumanoidRootPart.Position).unit * 40
	local ray = Ray.new(origin, direction)
	
	local hit, pos = workspace:FindPartOnRay(ray, ugly)
	
	
	if hit then
		if hit:IsDescendantOf(target) then
			return true
		end
	else
		return false
	end
end

local function findTarget()
	local players = game.Players:GetPlayers()
	local maxDistance = 40
	local nearestTarget
	
	for index, player in pairs(players) do
		if player.Character then
			local target = player.Character
			local distance = (ugly.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
			
			if distance < maxDistance and canSeeTarget(target) then
				nearestTarget = target
				maxDistance = distance
			end
		end
	end
	
	return nearestTarget
end

local function getPath(destination)
	
	local pathParams = {["AgentHeight"] = 5, ["AgentRadius"] = 2, ["AgentCanJump"] = false}
	
	local path = PathfindingService:CreatePath(pathParams)
	
	path:ComputeAsync(ugly.HumanoidRootPart.Position, destination.Position)
	
	
	
	return path
end	

local function attack(target)
	local distance = (ugly.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
	if distance > 4 then
		humanoid:MoveTo(target.HumanoidRootPart.Position)
	else
		local attackAnim = humanoid:LoadAnimation(script.Catch)
		attackAnim:Play()
		target.Humanoid.Health = 0
	end
end

local function walkTo(destination)
	
	local path = getPath(destination)
		
	if path.Status == Enum.PathStatus.Success then
		for index, waypoint in pairs(path:GetWaypoints()) do
			local target = findTarget()
		if target and target.Humanoid.Health > 0 then
			attack(target)
			break
		else
			humanoid:MoveTo(waypoint.Position)
			humanoid.MoveToFinished:Wait()
	      end
	  end
  else
	  humanoid:MoveTo(destination.Position - (ugly.HumanoidRootPart.CFrame.LookVector * 10))	
	end
end

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


while task.wait(0.5) do
	patrol()
end
1 Like

Hey, this might not fix your problem, but before i mention anything regarding the actual problem, you should know that

workspace:FindPartOnRay()

is deprecated.

Anyways, can you go more “in depth” with your problem?
Is the npc only chasing the target if its within 8 studs?

1 Like

The “Little stops” are caused by this. Because the waypoints are close to eachother, it does little stops as they are too close that the npc starts moving, then has to stop again. This is what I think is happening, as it has happened to me before. This may not be correct, however.

2 Likes

Oh yeah, and to answer that one question on the replacement for :FindPartOnRay(), im pretty sure you can just use Ray.Instance (returns the basepart that the ray touched

1 Like

alright maybe you are right my waypoints are near from each other but not that close I will try to make them far of each other, also I will add Ray.Instance as you say

Also, if this pathfinding code you are making is too complicated and you cant seem to fix it, you can use this module to make pathfinding easier. You can also hibernate some code, so it is more accessible. (Hibernating meaning, you can still check if player is in sight, as you wrote in your pathfinding code)

1 Like

Well I just make the waypoints far to each other but no changes, thank you for the pathfinding post btw

I believe your issue is a result of the player’s position (you) who has network ownership of your character, is updating too quick for the server. Your movement takes a minute to replicate to the server.

I had similar issues before, and it’s extremely identical.

1 Like

I see, what should I try to add to the script then? did you get a solution to that issue? @Nickaladormz

Can you explain me about that, and is there a solution about this? @Nickaladormz

No because it’s a bit too difficult to solve. I’m still looking for a simple solution, you can see my post here on the matter:

nvm, I just solved it, the problem was my custom scale

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