Raycast powered NPC pathfinding script doesn't work

I’m trying to make a NPC which’ll follow the nearest player but mine stops while following and it’s very easy to get rid of its’ following. That was the best I could do and there aren’t really good tutorials for this so I need help.

-- Services
local pathfindingService = game:GetService("PathfindingService")
-- Config
local aggroDist = 10
-- Variables
local character = script.Parent.Parent
local humanoid = character:WaitForChild("Humanoid")
local HumanRoot = character:WaitForChild("HumanoidRootPart")
-- Tool
local tool = character.ClassicSword
-- Functions
local function createPath(hrp)
	local path = pathfindingService:CreatePath()
	path:ComputeAsync(hrp.Position,HumanRoot.Position)
	-- Waypoints
	local waypoints = path:GetWaypoints()
	if path.Status == Enum.PathStatus.Success then
		for _, waypoint in pairs(waypoints) do
			if waypoint.Action == Enum.PathWaypointAction.Jump then
				humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
			end
			humanoid:MoveTo(waypoint.Position)
			humanoid.MoveToFinished:Wait()
		end
	end
	path.Blocked:Connect(function()
		print("Path blocked")
		local newpath = pathfindingService:CreatePath()
		newpath:ComputeAsync(character:FindFirstChild("HumanoidRootPart"), hrp.Position)
		waypoints = path:GetWaypoints()
	end)
end
-- Ray
while wait(0.5) do
	local rayParams = RaycastParams.new()
	rayParams.FilterDescendantsInstances = {
		character
	}
	rayParams.FilterType = Enum.RaycastFilterType.Blacklist
	rayParams.IgnoreWater = false
	local result = workspace:Raycast(HumanRoot.Position, character.Head.CFrame.LookVector*aggroDist, rayParams)
	if result then
		if result.Instance:IsA("BasePart") then
			if result.Instance.Parent:FindFirstChild("Humanoid") then
				trackingChar = result.Instance.Parent
				local magnitude = math.abs((HumanRoot.Position - result.Position).Magnitude)
				if magnitude < aggroDist then
					coroutine.wrap(createPath)(result.Instance.Parent:FindFirstChild("HumanoidRootPart"))
				end
			end
		end
	end
end

Not sure about this, because I am pretty new when it comes to bots and pathfinding, but it could be because the bot is trying to move to multiple waypoints at the same time. Can you show us a clip of this so we know exactly what’s happening?

1 Like

(30 chars)

1 Like

Does this also occur if the bot is farther away? If so, it probably is because the bot is trying to move to multiple waypoints at the same time.

1 Like

Problem is with that, I already know that. But how can I fix?

1 Like

The reason that the bot is moving to different waypoints at once is because you are running this:

coroutine.wrap(createPath)(result.Instance.Parent:FindFirstChild("HumanoidRootPart"))

every half a second, without a way to cancel the function, which causes the bot to follow the waypoints of both coroutines. You could either run the function (after changing it so it can change goal position within itself) once, or cancel the old coroutine once you compute a new path.

1 Like

Why are you doing those raycasts? Right now the effect is that the enemy will only “see” players that are directly in front of it. If you’re trying to check for line of sight then your vector math for setting up the Ray isn’t quite right.

If you explain a bit more about what you’re trying to do then it’s easier to help.

1 Like

That ray is just a starting method, I like making those improvements later when I get a working script.

I’m basically trying to do something like Piggy’s bots but it follows the player when a player passes its’ aggro distance.