How can I move a Models Position with code?

Its kinda in the title but what I want to do is clone a model filled with parts (I will need to access each part of the model) and changes its position. (the model’s position). There seems not to be a position property on a model even when I set a primary part.

Any help will be Awesome! :smiley:

43 Likes

You need to use MoveTo or SetPrimaryPartCFrame. The difference between the two is that MoveTo changes the model’s position with a Vector3s, while SetPrimaryPartCFrame changes the model’s position and orientation with a CFrame

local model = script.Parent
model:MoveTo(Vector3.new(0, 10, 0)) -- move 10 studs up in the y-axis from the origin
model:SetPrimaryPartCFrame(CFrame.new(0, 20, 0) * CFrame.Angles(0, math.rad(45), 0)) -- move 20 studs in the y-axis from the origin and rotate 45 degrees in the y-axis

Update: Note that SetPrimaryPartCFrame is deprecated in favour of the PivotTo function based on the model’s pivot offset

126 Likes

To move a model you need to set a Primary Part. Once you have that you just need to use SetPrimaryPartCFrame and the whole model will move.

7 Likes

That would be model:SetPrimaryPartCFrame(). There is more information here: Model | Documentation - Roblox Creator Hub, but in short you can do this:

model.PrimaryPart = --specific part here (you can also set this in the properties window)
--then do something like:
model:SetPrimaryPartCFrame(CFrame)

EDIT: Please note that with SetPrimaryPartCFrame() you can move your model inside of something (good or bad depending on use case), but if you want to avoid this please use Model:MoveTo(Vector3) as @Raretendoblox has said. MoveTo moves it up until it no longer collides with anything

16 Likes

Mine isn’t.
In my scenario, this moves only the primary part.
I’m personally trying to move a model out of replicated storage and into Workspace, but it will only move the primary part.

2 Likes

Can’t you just do this?

local model = game:GetService("ReplicatedStorage"):WaitForChild("Model")

model.Parent = workspace

I believe that this topic is about changing the CFrame position of a model, not where it is located.

4 Likes

This worked after some tinkering. Thank you, this was a pain to get working.

1 Like

Just so you know setprimarypartcframe is deteriorating so we now get to use :PivotTo (it’s basically the same thing)

2 Likes