Best way to smoothly follow paths using PathFindingService?

You can write your topic however you want, but you need to answer these questions:

Hi there! I am trying to have a model smoothly move to the nearest player using the pathfindingservice of Roblox. However right now the :PivotTo() is extremely blocky and TweenService makes the model move through the walls. Ass stated before I have tried using TweenService and :PivotTo(), and I am running out of ideas. The entire script of the model is

local Players = game:GetService("Players")
local PathfindingService = game:GetService("PathfindingService")
local TweenService = game:GetService("TweenService")
local Main = script.Parent
local BeingLookedAt = false
local Humanoid = Main:WaitForChild("Humanoid")




local function FindNearestPlayer()
	local NearestPlr = nil
	local ShortestDistance = math.huge

	for _, Player in ipairs(Players:GetPlayers()) do
		local Character = Player.Character
		if Character then
			local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
			if HumanoidRootPart then
				local Distance = (HumanoidRootPart.Position - Main.Position).Magnitude
				if Distance < ShortestDistance then
					NearestPlr = Player
					ShortestDistance = Distance
				end
			end
		end
	end
	return NearestPlr
end

local function MoveToNearestPlayer()
	local NearestPlr = FindNearestPlayer()
	local Char = NearestPlr.Character
	local HumanoidRootPart = Char.HumanoidRootPart

	local Path = PathfindingService:CreatePath({
		AgentRadius = (Main.Size.X + 2),
		AgentHeight = (Main.Size.Y + 1),
		AgentCanJump = false,
	})

	Path:ComputeAsync(Main.Position, HumanoidRootPart.Position)

	if Path.Status == Enum.PathStatus.Success then
		local Waypoints = Path:GetWaypoints()


		for _, Waypoint in pairs(Waypoints) do
			local Part = Instance.new("Part")
			Part.Size = Vector3.new(0.5, 0.5, 0.5)
			
			Part.Position = Waypoint.Position
			Part.CanCollide = false
			Part.Transparency = 0
			Main.Parent:PivotTo(Part.CFrame)
			Part.Parent = workspace
			local CF = CFrame.lookAt(Main.Position, Char.Head.Position)
			local x, y, z = CF:ToOrientation()
			
			
			print(Main.Orientation)
			print(x, y, z)
			
			Main.CFrame = CFrame.new(Main.CFrame.Position) * CFrame.fromEulerAngles(0, y, 0, Enum.RotationOrder.XYZ)
			
			task.wait(0.1)
		end
	else
		print("Pathfinding failed:", Path.Status)
	end
end

while task.wait() do
	MoveToNearestPlayer()
end

If anyone has any ideas feel free to share them, I am always open to suggestions and input!

3 Likes

Use Run Service so it will make script fire every frame (client only) so you can move player or hange player velocity to platform velocity.

By using RunService, wont the model just teleport from waypoint to waypoint? I am looking to have the model “walk” to the next point avoiding obstacles.

if you’re moving a humanoid rig why not use the MoveTo function in humanoids? it which makes the humanoid walk to a given position, as if the player was walking

Then check for part.Position:Changed() so it will fire everytime position of part changes but you will need to add wait for this or just use loop.