Pathfinding Service | Part not moving smooth enough

How do I get this part to smoothly move from start to end without stopping and then moving again like it does in the video??

wait(math.random(1,3))

print("Starting")
local pathfindingService = game:GetService("PathfindingService")
local TweenService = game:GetService('TweenService')

local model = script.Parent
local Start = model["1"]
local End = model["2"]

local pointModel;

if not model:FindFirstChild("PointModel") then
	local inst = Instance.new("Model")
	inst.Name = "PointModel"
	inst.Parent = model
	
	pointModel = inst
else
	pointModel = model:FindFirstChild("PointModel")
end

local function createPath()
	local Path = pathfindingService:CreatePath()
	Path:ComputeAsync(Start.Position, End.Position)

-- clear old path visualization
	pointModel:ClearAllChildren()
	

	local count = 0
	local points = Path:GetWaypoints()
	for _, point in ipairs(points) do
		local part = Instance.new("Part")
		part.FormFactor = Enum.FormFactor.Custom
		part.Size = Vector3.new(0.4,0.4,0.4)
		part.Position = point.Position
		part.Anchored = true
		part.CanCollide = false
		part.Parent = pointModel
		count = count + 1
		part.Transparency = 0
		part.Name = count
	end	
end


while #pointModel:GetChildren() == 0 do
	print("trying")
	createPath()
	task.wait(1)
end

print(#pointModel:GetChildren())

task.wait(5)


local pathPoints = {}

if pointModel then
	for _, child in ipairs(pointModel:GetChildren()) do
		if child:IsA("Part") then
			table.insert(pathPoints, child.Position)
		end
	end
end
print("PATH POINTS = " .. #pathPoints)

-- Customize your BasePart and set its initial position
local basePart = Start
basePart.Anchored = true -- Prevent physics from affecting it
basePart.CanCollide = false -- Avoid collisions with other objects
local currentPositionIndex = 1

local TweenService = game:GetService("TweenService")

local function moveToNextWaypoint()
	local targetPosition = pathPoints[currentPositionIndex]
	local distanceToTarget = (targetPosition - basePart.Position).Magnitude

	if distanceToTarget > 1 then -- Tolerance for reaching the next point
		local tweenInfo = TweenInfo.new(1) -- Adjust this for your desired speed
		local goal = {}
		goal.Position = targetPosition

		-- Calculate the direction from the BasePart to the target position
		local direction = (targetPosition - basePart.Position).Unit

		-- Adjust the orientation of the BasePart to face the movement direction
		local orientation = CFrame.new(basePart.Position, basePart.Position + direction)

		-- Use TweenService to smoothly interpolate the position
		local tween = TweenService:Create(basePart, tweenInfo, goal)
		tween:Play()

		-- Update the orientation directly
		basePart.CFrame = orientation

		-- Wait for the tween to complete
		--tween.Completed:Wait()
	else
		-- Reached the target point, move to the next one
		currentPositionIndex = currentPositionIndex + 1

		if currentPositionIndex <= #pathPoints then
			-- Wait briefly before moving to the next point (adjust as needed)
			wait()
			moveToNextWaypoint() -- Call the function recursively for the next waypoint
		end
	end
end

local function MoveAlongPath()
	while currentPositionIndex <= #pathPoints do
		moveToNextWaypoint()
		wait(0.01) -- Control the update rate of the movement
	end
end

-- Start moving along the path
MoveAlongPath()
print("DONE")

1 Like

set your tweenstyle to linear, if you want to be fancy you can add tweenstyles at the beginning and end to make it start and stop smoothly

did not work unfortunately. It just did the same thing

it’s cause you’re playing a tween every 0.01 seconds regardless of if it has finished or not, you need to yield so the tween actually finishes.