Pathfinding in tutorial not working properly

Hi, I recently tried following the official roblox documentation tutorial to make a pathfinding dummy, and it is absolute trash, this is not what I wanted, can someone please help me?

local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local RunService = game:GetService("RunService")

local path = PathfindingService:CreatePath()
local Target = nil
local Event = ServerStorage:WaitForChild("SoundCreated")
local PlayersCharacters = {}
local character = script.Parent
local HRP = character.HumanoidRootPart
local humanoid = character:WaitForChild("Humanoid")
local partToFollow = nil

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {character}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude

Players.PlayerAdded:Connect(function(player)
	table.insert(PlayersCharacters, player)
end)

Players.PlayerRemoving:Connect(function(player)
	table.remove(PlayersCharacters, table.find(PlayersCharacters, player))
end)

wait(1)

local function followPath(destination : Vector3, Part)
	-- Compute the path
	
	local waypoints 
	local nextWaypointIndex
	local reachedConnection
	
	if destination and Part then
		
		local success, errorMessage = pcall(function()
			path:ComputeAsync(HRP.Position, destination)
		end)
		
		if success and path.Status == Enum.PathStatus.Success then
			waypoints = path:GetWaypoints()
			
			-- Detect when movement to next waypoint is complete
			if not reachedConnection then
				reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
					if reached and nextWaypointIndex < #waypoints then
						-- Increase waypoint index and move to next waypoint
						nextWaypointIndex += 1
						humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
					else
						reachedConnection:Disconnect()
					end
				end)
			end

			-- Initially move to second waypoint (first waypoint is path start; skip it)
			nextWaypointIndex = 2
			
			if waypoints[nextWaypointIndex] then
				humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
			end
			
		end
		
	end
end

RunService.Heartbeat:Connect(function()
	
	local Closest = math.huge
	local HasFound = false
	local ClosestChar = nil
	
	for _, plr in PlayersCharacters do
		local char = workspace:WaitForChild(plr.Name)
		local HrpTarg = char:WaitForChild("HumanoidRootPart")
		
		local raycastResult = workspace:Raycast(HRP.Position, HrpTarg.Position, raycastParams)
		
		if raycastResult then
		else
			
			if ClosestChar == nil and HasFound == false then
				HasFound = true
				ClosestChar = char
				Closest = (HRP.Position - HrpTarg.Position).Magnitude
			elseif (HRP.Position - HrpTarg.Position).Magnitude < Closest then
				HasFound = true
				ClosestChar = char
				Closest = (HRP.Position - HrpTarg.Position).Magnitude
			
			end
			
		end
		
	end
	
	if Closest < math.huge and HasFound and ClosestChar then
		partToFollow = ClosestChar.HumanoidRootPart
	else
		partToFollow = nil
		humanoid:MoveTo(HRP.Position)
	end
	
	if partToFollow then
		followPath(partToFollow.Position, partToFollow)
	else
		followPath(HRP.Position, HRP)
		humanoid:MoveTo(HRP.Position)
	end
	
end)

Could you please provide a little more information on the specifics of your current issue so I am able to help you?

If I use the original pathfinding script, like just copy and pasting the script from the pathfinding tutorial on the official Roblox documentation, it works normally, however, I want to “pathfind” every single frame so that the dummy moves accordingly when the player changes position, so when I tried doing that, it messed up the pathfinding like in the video.