Pathfinding NPC not moving(simple path)

I have a NPC that chases the player around. I am using simple path. But when I use the code, it returns computation error. After Debugging a bit, i found out that when simplepath was computing the path, there was zero waypoints in the path. Any help?

The module i am using:

--local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--local RunService = game:GetService("RunService")
local ServerStorage = game:GetService("ServerStorage")
local TweenService = game:GetService("TweenService")
local Workspace = game:GetService("Workspace")
local raiderStats = require(ServerStorage.RaiderStats)
local simplepath = require(ReplicatedStorage.Packages.simplepath)
local raider = {}

local function findNearestPlayer(position)
	local nearestPlayer, nearestDistance
	local maxDistance = math.huge
	for _, player in pairs(Players:GetPlayers()) do
		local character = player.Character
		local distance = player:DistanceFromCharacter(position)
		if not character or distance > maxDistance or (nearestDistance and distance >= nearestDistance) then
			continue
		end
		nearestDistance = distance
		nearestPlayer = player
	end
	return nearestPlayer.Character
end

raider.spawn = function(name, spawnIndex)
	local model = ReplicatedStorage:WaitForChild("Raiders")[name]:Clone()
	local map = Workspace:WaitForChild(ReplicatedStorage.Map.Value)
	model:PivotTo(map.raidSpawns[tonumber(spawnIndex)].CFrame)
	model.Parent = map.raiders
	local modelStats = raiderStats.get(name)
	local targetUpdater = nil
	local target = nil
	--local targetdistance = nil

	local function tween(part, destination)
		local tweenBase = TweenService:Create(
			part,
			TweenInfo.new((part.Position - destination.Position).Magnitude / modelStats["speed"]),
			{ Position = destination.Position }
		)
		tweenBase:Play()
		tweenBase.Completed:Wait()
	end

	targetUpdater = task.spawn(function()
		while true do
			target = findNearestPlayer(model.PrimaryPart.Position)
			task.wait()
		end
	end)

    local Path = simplepath.new(model)

	Path.Visualize = true

    local blockedConnection
    local errorConnection
    local reachedConnection
    local waypointConnection


	--If the path is blocked
	blockedConnection = Path.Blocked:Connect(function()
        print("runningblocked")
		Path:Run(target.PrimaryPart)
	end)

	--In case of an error
	errorConnection = Path.Error:Connect(function()
        print("runningerror")
		Path:Run(target.PrimaryPart)
	end)

	reachedConnection = Path.Reached:Connect(function(model, finalWaypoint)
        print("runningreached")
		tween(model.PrimaryPart, finalWaypoint.Position)
		Path:Run(target.PrimaryPart)
	end)

	waypointConnection = Path.WaypointReached:Connect(function(model, _, nextWaypoint)
        print("runningwaypointConnection")
		tween(model.PrimaryPart, nextWaypoint.Position)
		Path:Run(target.PrimaryPart)
	end)
    print(target.name)
    Path:Run(target.PrimaryPart)
    print(Path.LastError)
	local healthConnection = model.health:GetPropertyChangedSignal("Value"):Connect(function()
		if model.health.Value <= 0 then
			blockedConnection:Disconnect()
            errorConnection:Disconnect()
            reachedConnection:Disconnect()
            waypointConnection:Disconnect()
            healthConnection:Disconnect()
            task.cancel(targetUpdater)
            model:Destroy()
		end
	end)
end

return raider

1 Like

Any help? this is bugging me a lot