Tweening a part?

So, I’d like to tween a part to a location, but not a specific location, like a couple studs to the right/left. Is there any way I could achieve this without having to set the position specifically? If this is too vague, please tell me, I’m not very good at describing things.

3 Likes

Just add a Vector3 to it’s position. For example:

part.Position = CFrame.new(part.Position + Vector3.new(0,0,1))

If you’d like to acheive this in a smooth-motion like tween [without actually using a tween]. You could use a for i loop :

for i = 1,10 do
    part.Positon = CFrame.new(part.Position + Vector3.new(0,0,0.1))
end

That would move the part 0.1 studs / 10 times, which equals 1 stud

This is a very simple way to do it, if you’d like to use the new tween API you can find information about that here:

7 Likes

Nice explanation, but when actually setting the Property of the part itself, you would have to Set the CFrame property to a CFrame value, not set the Position property to a CFrame value

2 Likes

Yes you’re correct! I’m not sure which one it is, but one preserves orientation correct?

I think using .Position preserves orientation, I might be wrong on that.

2 Likes

It would, however it also accounts for collision, so if there are objects in the way, the tween/loop might get obstructed (assuming cancollide is true)

2 Likes

Really? I’ve never experienced that before. Usually it just goes through all the parts lol

2 Likes

This is no longer true, there was an announcement about this way back when. It’s about halfway down the post I think:

2 Likes

hmm, interesting, thanks for showing me this :slight_smile:

1 Like

Hey, I know I’m a bit late, but how would I change the speed/length? In short, how do I change how far it goes?

If you’re using the code from earlier, the for loop would determine how long/how far the movement.

for i = 1,10 do -- The 10 here would determine how far/how long
    part.Positon = CFrame.new(part.Position + Vector3.new(0,0,0.1))
end

Changing the 10 in the code would subsequently change how far it moves (0.1 studs for every number after ten), thus increasing the time that it would take to complete. If you want it to move more than the code provides, just increase the 10.

1 Like

to change the length, with a for loop, you would simply change the wait time, for example:

for i = 1,10 do -- The 10 here would determine how far/how long
   part.CFrame = CFrame.new(part.Position + Vector3.new(0,0,0.1))
   wait(1/10)
end

which would wait 1 second for the whole loop, there being 10 iterations of the loop, and waiting 1/10 seconds per iteration, 10* 1/10 = 1, would equate out to one second

Generally the wait time per iteration would be the total amount of time divided by the number of iterations

That’s what I assumed, I was not certain though, thanks!

Not sure if this might be taking it off topic, But I’ve had an issue when I tried to use this today.

Video:

As you can see, the part shrinks and is now unanchored, There’s nothing in the output, Help?

Edit2: I just tested it with a different script and it does the same thing.

Could you put your script? use the format if u know how to

Do you want the whole thing? Some of it isn’t really related to the movement.

I just realized that I’m an idiot and had a part above the door that wasn’t anchored and is that size, which made the illusion. herpaderp.