Need help with tweening to nearest part

In every while true loop, create a variable called nearestDistance = math.huge and a variable for the actual Instance, each for loop through the nodes, check distance between and compare if it is less than the current nearest distance, if it is, set nearest distance to that distance and the part variable to said part, after the for each loop is complete , play the tween. Goofy ahh

Try this:

local mover = script.Parent.Part
local Waypoints = game.Workspace.Nodes:GetChildren() -- This is a folder that contains all of the path bricks
local TweenService = game:GetService("TweenService")

local info = TweenInfo.new(
	2,                     
	Enum.EasingStyle.Quad   ,  
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

table.sort(Waypoints, function(a, b)
	-- Sorts the table such that the closest parts to the mover are first
	-- If it doesn't work try changing the > with an <
	return (a.Position - mover.Position).Magnitude > (b.Position - mover.Position).Magnitude
end)

for _, node in ipairs(Waypoints) do
	TweenService:Create(mover, info, {Position = node.Position}):Play()
end

For this, it only locks on to a singular path and if I try to move it near another one, it just instantly moves back to the old path.

So you want to recompute the path everything the mover changes position?

Yes, that is correct.

I want it to complete the entire path

Try this:

local mover = script.Parent.Part
local Waypoints = game.Workspace.Nodes:GetChildren() -- This is a folder that contains all of the path bricks
local TweenService = game:GetService("TweenService")

local info = TweenInfo.new(
	2,                     
	Enum.EasingStyle.Quad   ,  
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

local function computeAndMove()
	table.sort(Waypoints, function(a, b)
		-- Sorts the table such that the closest parts to the mover are first
		-- If it doesn't work try changing the > with an <
		return (a.Position - mover.Position).Magnitude > (b.Position - mover.Position).Magnitude
	end)

	for _, node in ipairs(Waypoints) do
		TweenService:Create(mover, info, {Position = node.Position}):Play()
	end
	
	task.wait(1) -- waits a bit so you can move the mover
-- change the 1 to 5 if you need more time
	
	computeAndMove()
end


1 Like

The same exact thing happens, I think it’s because it doesn’t remove the brick from the table

Hi, sorry for late response. Can you show me a video of the game with the script running please?

There’s no need, I got it to work

1 Like

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