How do I make a part follow a set of nodes using CFrame?

I’ve been searching the forum for so long, and none of it makes sense to me. I just want to make a part follow invisible nodes using CFrame to move it. This is like my third time posting something about this, so I would really like some help on what exactly to do. I’m really confused and don’t know where to start. Thank you.

3 Likes

If you want a part to move between a group of points, you can use CFrame:lerp(EndCFrame,integer) to smoothly move between points.

function smoothMove(endCF)
    for i = 0,1,0.1 do
        Part.CFrame = part.CFrame:lerp(endCF,i)
        wait(.1)
    end
end

However lerp can seem choppy at times, especially with long distances as it doesn’t consider the distance, but it just adds 10% of the distance at a time.

You can use the TweenService to smoothly alter the CFrame of the part.

When it comes to moving through the groups of points, you can simply loop through a list of parts and either use TweenService or Lerping to move between them.

1 Like

So I would just put this in the model that I want to move? Then I would just make a set of nodes and it would follow it? Sorry, I’m new to developing and this is a little confusing. Can you maybe explain this more a little bit? Thank you for replying! (Also there would be multiple nodes, not just a start and and end.)

That code won’t work on it’s own as it’s just meant as an example, but I suggest you read up on CFrame:lerp(). and TweenService.

As for looping through the parts, you should use a

for i,v in pairs(LIST) do -- This will loop through the individual parts in the model, where v is the object
end

There’s 2 things I’m not understanding. The first is how to make it automatically move to the next part by checking the name of the part in the Model ( The parts would be named in Number order of the nodes). The second is how to apply TweenService to this, I rarely ever use TweenService. The Code I have so far just gives me “Argumente missing or nil”. Here’s my code

local Part = script.Parent
local f = Part.CFrame
local tweenservice = game:GetService("TweenService")
    for i = 0,1,0.1 do
        local tween = tweenservice:Create(Part, {f = f:lerp(endCF,i)})
	tween:Play()
    wait(.1)
    end

I feel like I’m asking too much, sorry! If you can point me in anymore of a right direction, thank you. :slight_smile:

You should either use tweenService or Lerp don’t use both at the same time, if you’re going to be using tweenservice;

local tween = tweenService:Create(Part,tweeninfo.new(1),{CFrame = newCFrame})
tween:Play()

the create functions takes 3 arguments, the instance you want to effect, the tweeninfo.new(Time), where time is how long it should take to complete the tween, and the third argument is a dictionary to say what properties you want to change, which may looks like this {CFrame = CFrame.new(0,10,0)}.

After you run :Play() on the tween, the tweenService will smoothly change the specified properties you stated.

You don’t need

for i = 0,1,0.1 do
end

As the tweenService handles that for you.

If you’re quite new to scripting you should start tweening between two points until you get the idea of how tweening works

Okay, I’ll try this. Thank you!

Heya! Hate to revive this thread, but I am following in the same footsteps as OP right now.

I am creating an attraction system where the vehicle follows a set of points.

However, I have tried tweening as @Death_Defiance has suggested, however it doesn’t seem to be accomplishing my intended goal.

local tweenService = game:GetService("TweenService")
local Part = workspace.Vehicle
local pointsChildren = game.Workspace.Points:GetChildren()

local tweenInfo = TweenInfo.new(
    2, -- Time
    Enum.EasingStyle.Linear, -- EasingStyle
    Enum.EasingDirection.Out, -- EasingDirection
    0, -- RepeatCount (when less than zero the tween will loop indefinitely)
    false, -- Reverses (tween will reverse once reaching it's goal)
    0 -- DelayTime
)

wait(5)

for i = 1,#pointsChildren do
    local tween = tweenService:Create(Part,tweenInfo,{CFrame = CFrame.new(i)})
    tween:Play()
end

1 Like

CFrame.new requires at least 3 parameters I believe
Please do correct me if I’m wrong, I don’t use CFrames often

a CFrame contains global X , Y , and Z coordinates as well as rotation data for each axis. In addition, CFrames contain helpful functions for working with objects in the 3D space.

I also tried that as an alternative route by providing the position, and that errored to:

[14:51:34.885 - ServerScriptService.Script:17: attempt to index number with 'Position'](rbxopenscript://www.dummy.com/dummy?scriptGuid=%7B2b7db3d7-5343-4680-b857-c12bcd453330%7D&gst=3#17)
14:51:34.885 - Stack Begin
[14:51:34.885 - Script 'ServerScriptService.Script', Line 17](rbxopenscript://www.dummy.com/dummy?scriptGuid=%7B2b7db3d7-5343-4680-b857-c12bcd453330%7D&gst=3#17)
14:51:34.886 - Stack End
1 Like

Putting # before a table just returns the number of indexes inside of that table, it doesn’t actually index them. So you’re probably getting a number value instead of a Part instance.
Please refer to this article

1 Like

Alright, so I’ve tried to gather all the points into a table and tried tweening from that table. But now that’s resulted into nothing happening and the output printing nothing.

local systemPoints = {}

for i, v in pairs(pointsChildren) do
    table.insert(systemPoints,v)
end

wait(5)

for _, p in pairs(systemPoints) do
    local tween = tweenService:Create(Part,tweenInfo,{CFrame = CFrame.new(p.Position)})
    tween:Play()
end

Could you try removing the {} wrapping the position? Not sure if it would work but it’s a shot

1 Like

Try letting the for loop wait for the tween to finish before going through the next loop?

1 Like

Unfortunately, that resulted into an error message, but hey the output is printing again, so that’s a good sign haha!

I’ve tried out that suggestion but unfortunately nothing panned out. Do you think there could be something wrong with the tweening instead?

Yeah, I’ve applied my own code that might fix your problem:


local systemPoints = {}

for i, v in pairs(pointsChildren) do
    table.insert(systemPoints,v)
end

wait(5)

for i = 1, #systemPoints do
    local Goal = {}
   Goal.Position = systemPoints[i].Position 

    local tween = tweenService:Create(Part,tweenInfo,Goal)
    tween:Play()

    tween.Completed:Wait() -- waits for the tween to be finished 
end

Also, make sure your part or whatever you are moving is anchored.

2 Likes

Thanks a million! that seemed to work out just perfectly!

It still needs refinements and I’ll have to figure out how it’s going to rotate itself for the player, but overall I’m satisfied with the result! Thanks a lot!

2 Likes

Do you want the part to also rotate to every white part? Because in the video it just moves its position which doesn’t look quite nice.

1 Like

I didn’t want you to do more work than you’re already doing! Haha! But I had an idea of giving turning parts certain labels that the script would recognize and it would either turn clockwise or counter clockwise.

But to answer the question, yes, now my next agenda is executing the turning points. I’m going to reorganize the system points though to make it have more speed.

1 Like