Pathfinding combined with tweens doesn't work as expected

Trying to move a model using tweens for each part by moving it by adding the distance from endPart to its vector position.

function followPath(destination, part)
	-- Compute the path
	local success, errorMessage = pcall(function()
		path:ComputeAsync(part.Position, destination)
	end)

	local tweenPart

	if success and path.Status == Enum.PathStatus.Success then
		waypoints = path:GetWaypoints()
		
		for i, v in pairs(waypoints) do
			local node = Instance.new("Part")
			node.Size = Vector3.new(1,1,1)
			node.Position = v.Position
			node.Anchored = true
			node.CanCollide = false
			node.Parent = workspace
			
			local dist = v.Position - part.Position
			tweenPart = TS:Create(part, TweenInfo.new(dist.Magnitude / 10, Enum.EasingStyle.Linear), {Position = Vector3.new(part.Position.X - dist.X, part.Position.Y - dist.Y, part.Position.Z - dist.Z)})
			tweenPart:Play()
			task.wait(dist.Magnitude / 10)
			print("a")
		end
	else
		warn("Path not computed!", errorMessage)
	end
end

function walk(model)
	canDoAiThings = false
	local area = model.Parent
	local aiParts = area.AiParts:GetChildren()
	local randomPart = aiParts[math.random(1, #aiParts)]

	local dist = (model.PrimaryPart.Position - randomPart.Position)

	for i, v in pairs(model:GetDescendants()) do
		if v:IsA("Part") or v:IsA("BasePart") or v:IsA("MeshPart") then
			followPath(randomPart.Position, v)
		end
	end
	
	task.wait(dist.Magnitude / 10)
	canDoAiThings = true
end

https://gyazo.com/7c7162b5670f142889ed0dd722b266b2

Sometimes the model moves, but like halfway of its first waypoint

Update (on the server, doesnt show on client tho?):
https://gyazo.com/260d6a1a63b0e92a3743bffdd7ff3d75

try replacing

task.wait(dist.Magnitude / 10)

with

tweenPart.Completed:Wait()

if it doesn’t work then it might mean the problem is the position you set as the tween goal

1 Like

Something was wrong with the rig, i’ve fully remade the rig and rerigged it and it works now

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.