Need help with tweening to nearest part

I want to know how to make a part, tween to the nearest ‘waypoint’, like a path.

This is what I’ve got so far, but it doesn’t seem to be working, could anyone help me?

local mover = script.Parent
local Waypoints = game.Workspace.Nodes -- 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
)


while true do
	for i, v in pairs(game.Workspace.Nodes:GetChildren()) do
		local Distance = (script.Parent.Part.Position - v.Position).magnitude
		if Distance > 10 then
		    TweenService:Create(script.Parent.Part, info, v.CFrame)
		end
	end
	wait()
end

The code block from here might help.


To explain further:

  • You should be iterating through the descendants of the workspace, not its children.
  • The result of (Vector3 - Vector3).Magnitude can be negative, so you should confirm that the integer returned is positive by passing it through math.abs().

But I wasn’t getting children of the workspace, I was getting it of the folder

He has commented that nodes is a folder , also you do not need to do math.abs after doing .Magnitude, it already returns the positive length, goofy ahh

My fault, didn’t read the post properly @HearTheThing.

Also apparently didn’t think when passing to math.abs(). My fault again!

Re-reading OP’s post, I imagine that the issue is that they’re not playing the tween.

local tween : Tween = TweenService:Create(script.Parent.Part, info, v.CFrame)
tween:Play()
tween.Completed:Wait()

My fault for the inconsistencies!

That’s not the issue, it says 'Cannot cast to dictionary."

It would have been an issue in the future.

Tween’s use dictionaries when interpolating properties of objects:

TweenService:Create(script.Parent.Part, info, v.CFrame)

-- should be

TweenService:Create(script.Parent.Part, info, {CFrame = v.CFrame})

… in conjunction with playing the tween, as mentioned in my previous post.

Goofy ahh boy it supposed to be just {CFrame = v.CFrame}

1 Like

Oh I completely forgot about that, thank you

Solved it either way. Thanks for the help.

One more thing, it works but when it reaches its destination, it instantly goes back to the one it was at before. I’m guessing this is because it moves to the goal, and because it moved there the start location is now 10+ studs away and it moves back. Would you know how to fix this?

I’d approach it by removing the nodes once your part has tweened to them:

local tbl = nodes:GetChildren()

for k : number, v : Part in pairs(tbl) do
    -- tween the part to the node's position

    -- remove the node from the node list
    table.remove(tbl, k)
end

That way, your part should continuously move to the next node.

Hope this helps.

Alright, but would there be a way to add it back to the table?

If you’re iterating through all your nodes in a loop, then a new table will be generated every loop.

Therefore, you don’t need to worry about reinserting nodes back into the table:

while true do

    -- This is reassigned every loop
    local tbl = nodes:GetChildren()

    -- code to loop through your table here
end

Hope this helps.

This works, but one VERY LAST thing.
image

This is the way I want the tween to move, however, it moves like this:
image
Would I have to use a pathfinding NPC to get my result, or is there a way for it to work like the desired result.

Hi, which image is your desired path please?

The one where it moves -------> then down is my desired, the diagonal one is the undesired result.

In which case, I would consult my earlier link. It’s preferable to use pathfinding in a situation like this, though if you would prefer to just iterate through your nodes, then I suggest using an ordered table, where you can tween between each node individually and in order.

1 Like

Hi again, in your script i see that you have a variable called mover but you never use it,

i don’t know what’s script.Parent.Part.

Im confused on which one you’re supposed to tween

Was a mistake, I forgot to remove it sorry.

1 Like