How to move multiple parts with GetChildren()[math.random()]

I’m trying to move a part with a model inside of it with other parts that I want to all move together simultaneously here:

image

Here’s my code, it works but it’s extremely laggy and isn’t in sync with the main “platform” part.

image

Example of what’s happening:

https://gyazo.com/a9d6a559449a8cecb9004a3ee1aefd17

This is a really stupid question but I just need some help please!

1 Like

Im not 100% sure i understand the question, do you want to move the whole model at once or random parts from the model ?

The reason the parts arent in sync is because you are choosing a random part each loop and moving it

If you are trying to move the whole model at once you can either use SetPrimaryPartCFrame or loop through all the parts and move them individually,
dont use SetPrimaryPartCFrame if you intend to move the model by a lot and often as floating point errors make the model drift apart slowly.

2 Likes

I would like to move the whole thing at once, I was just trying to get them individually since I thought that would work.

Where would I put SetPrimaryPartCFrame in?

1 Like

Yeah, first you have to set the PrimaryPart of the model. Once you do that, you just use SetPrimaryPartCFrame on the model instead of setting the CFrame of a part:

model:SetPrimaryPartCFrame(model:GetPrimaryPartCFrame() * CFrame.new(0, 0, 0.5))
3 Likes

Thanks dude!

1 Like

Normally, if you want to move a model around on the server, with all of its parts staying oriented the same relative to each other, you weld everything together and use some type of body mover on one of the parts.

There are some special cases where you might want to use SetPrimaryPartCFrame() to move a model, but this is generally when you want to move it in a single jump and you don’t have anything interacting with the model that isn’t welded to it. For example, if you have a tool to place furniture in a room, cloning and CFraming the models into position is fine.

But it’s not usually desirable to do server-side animation by setting CFrames, for a couple of reasons. One reason is that the resulting animation runs at replication rate which is not a nice smooth framerate. A model moving with body movers gets client-side interpolation so that it animates smoothly at the rendering framerate. Another issue with CFrame animation is that it doesn’t interact with physics simulation the way you might expect, for example, a player standing on a CFrame-animated moving platform will not move with the platform, it will slide out from under them; as far a the physics system is concerned, such a platform does not have velocity, it’s making discrete, instant jumps (mini teleports).

7 Likes

How do I set the PrimaryPart?

1 Like

Nevermind, figured it out.

Definitely what @Crazyman32 said… but if you are doing it many many many times, then it will slowly get offset so all your parts won’t move in unison (here is where I found that info: (PrimaryPart_CFrame_Error) But I’ve never had the issue so you’re pretty safe with it. :slight_smile:

2 Likes

To that point, @sircfenner submitted some code to my cookbook that keeps a record of the original CFrame values to avoid the error, and thus allows you to continuously move it accurately:

11 Likes

Then nevermind what I said. :stuck_out_tongue: That’s some real nice code.

1 Like