NPCs never move along a path?

i’m trying to make a LocalScript that will allow tagged npcs to move along a path. yet when i call the function and try to make them chase the player, they don’t budge. why is that?

-- subspace tripmine artificial intelligence

local players = game:GetService("Players")
local pathfinding_service = game:GetService("PathfindingService")
local collection_service = game:GetService("CollectionService")
local run_service = game:GetService("RunService")

local client = players.LocalPlayer
local character = client.Character or client.CharacterAdded:Wait()
local humanoid = character:FindFirstChildOfClass("Humanoid")

local tag = "doofus"

local agent_parameters = {
	AgentRadius = 3,
	AgentHeight = 6,
	AgentCanClimb = true,
	Costs = {
		Climb = 2,
		Water = 20,
	}
}

function move_to(npc, target_pos)
	local path = pathfinding_service:CreatePath(agent_parameters)
	local humanoid = npc:FindFirstChildWhichIsA("Humanoid")

	local waypoints
	local next_waypoint_index
	local reached_connection
	local blocked_connection

	local function follow_path(destination)
		local success, failure = pcall(function()
			path:ComputeAsync(npc.PrimaryPart.Position, destination)
		end)

		if success and path.Status == Enum.PathStatus.Success then
			print(`path created`) -- prints, even though npcs aren't moving
			waypoints = path:GetWaypoints()

			blocked_connection = path.Blocked:Connect(function(blocked_waypoint_index)
				if blocked_waypoint_index >= next_waypoint_index then
					blocked_connection:Disconnect()
					follow_path(destination)
				end
			end)

			reached_connection = humanoid.MoveToFinished:Connect(function(reached)
				if reached and next_waypoint_index < #waypoints then
					next_waypoint_index += 1
					humanoid:MoveTo(waypoints[next_waypoint_index].Position)
				else
					reached_connection:Disconnect()
					blocked_connection:Disconnect()
				end
			end)

			next_waypoint_index = 2
			humanoid:MoveTo(waypoints[next_waypoint_index].Position)
		else
			warn(`path not created due to {failure}`)
		end
	end

	follow_path(target_pos)
end

local path_connection
path_connection = run_service.RenderStepped:Connect(function()
	local tripmines = collection_service:GetTagged(tag)

	for i, trip_mine in ipairs(tripmines) do
		local target_position = character.PrimaryPart.Position
		move_to(trip_mine, target_position)
	end
end)

players.PlayerRemoving:Connect(function(plr)
	if plr == client then
		if path_connection then
			path_connection:Disconnect()
		end
	end
end)

1 Like

after a bit of tinkering, pathfinding appears to only work on the server (i could not get it to work on the client.)

1 Like

You can try to move stuff away like try it on one Npc get that script you have on it find what may be wrong it like mix it around a bit and then it may fox your problem