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
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().
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
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.
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.