Hello, I have two parts welded together, but when I move the part in a script the other doesn’t move. how do I fix it?
Are they part of the same model? If so you should probably be able to use Model:SetPrimaryPartCFrame (roblox.com).
I’m not quite sure, but I think the welded parts might move if you update their CFrame instead of position. If you are tweening it the following is a really good tutorial:
Introduction to Tweening Models - Resources / Community Tutorials - DevForum | Roblox
They are not in a model
It’s a part Inside a part.
But i don’t know how to tween CFrame
Here is the tweening part
TweenSrv:Create(Part,TweenInfo.New(3),{Position = Vector3.New(23,10,-89)}):Play()
CFrames get a rep of being more complicated than they are, think of them as extended Vector3’s.
Creating a new CFrame with a position? Simply do
local cf = CFrame.new(Vector3.new(0, 1, 0))
The above is equivalent to changing position to said Vector3. Another reason to use CFrames is rotation becomes really easy. Somtimes. Want a part to look at another part?
local cf = CFrame.new(thisPart.Position, otherPart.Position)
Tweening CFrame is very similar to tweening position. Same goes for all values.
Anyways, here’s how to tween CFrame:
TweenSrv:Create(Part, TweenInfo.new(3), { CFrame = CFrame.new(Vector3.new(23, 10, -89) }):Play()
But it tween the orientation, how can i prevent it
CFrames can be offset by Vector3’s.
In this case you have a specific position in mind, so you need to calculate the position change.
local positionDelta = target - currentPosition
TweenSrv:Create(Part, TweenInfo.new(3), { CFrame = Part.CFrame + positionDelta } )
The result of
Part.CFrame + positionDelta
will be your target position with the same rotation as you initially had.
Uhh, Could You please explain more
is, there an easier way? btw, thanks for replying.
Well, not sure which part you want me to explain
positionDelta
is simply the distance, as a Vector3, between the start and end position.
If its the CFrame you are unsure about, I’ll try to simplify it a bit.
Imagine a CFrame as a dual-Vector3d. One describing position, another orientation.
Lets pretend the following is your Part’s current CFrame.
CFrame == (Vector3.new(0, 0, 0), Vector3.new(0, 0, 10)
As I mentioned earlier you can add Vector3’s to CFrames. Lets say we want to move our part to the position (10, 10, 10)
and keep our rotation.
newCFrame = CFrame + Vector3.new(10, 10, 10)
newCFrame == (Vector3.new(10, 10, 10), Vector3.new(0, 0, 10))
As you can see, the new CFrame describes a new position with the same orientation. The reason we need to calculate positionDelta will become apparent when we try another coordinate. Lets say your Part is currently at (-10, 2, 3)
.
CFrame == Vector3.new(-10, 2, 3)
newCFrame = CFrame + Vector3.new(10, 10, 10)
newCFrame == (Vector3.new(0, 12, 13), Vector3.new(0, 0, 10))
As you can see, that didn’t really go as planned. We wanted it to end at (10, 10, 10)
.
What we are doing, basically, can be simplified to following math equation:
new = current + change
-- Since we are after the change we simply subtract current from new
new - current = change
-- The above statement is the same as the following
change = new - current
I hope this made sense. If not, just ask!
I Understand(Your explanation was great), But I am tweening a LOT of parts, Is there any easier way, sorry to bother you.
No worries, I am here because I want to help!
Are those parts you are moving welded together? If you tween the CFrame of one of the welded parts all of them should move. (I think.)