Model position and orientation does not change with primary part

I want in my build script to be able to move a whole model and change its orientation by using the primary part making all the parts change with the primary part.

What I’ve done is:

Model.PrimaryPart.Position = vector3.new( “whatever position” )

it moves the primary part but not the whole model. I can’t union as then it changes color and material. Please help!

8 Likes

Have you tried using Model:SetPrimaryPartCFrame()? It allows you to move the entire contents a Model at once, through the offset of each part to the Model’s PrimaryPart.

For instance, to set the position of the Model, you could do this:

Model:SetPrimaryPartCFrame(CFrame.new(Vector3.new( “whatever position” )) --you can also edit the orientation though the second parameter of the CFrame

If you don’t care about the orientation of the Model, you can also use Model:MoveTo().

7 Likes

You need to the change CFrame if you want it to include Position and Orienation.
You can use :SetPrimaryPartCFrame() as @ExcessEnergy said.

1 Like

Try this

Model.PrimaryPart:MoveTo(Vector3.new(1,1,1))
1 Like

Object:SetPrimaryPartCFrame(GetMousePlacementCFrame(Plot, Object)) this is what’s in my script and when you move your mouse the model doesn’t move all its parts

1 Like

You choose to move only the Primary Part, that is why. If you want it to move the entire model, use :MoveTo() as @FerbZides suggested. Be sure it will not include the Orientation.

1 Like

Object:SetPrimaryPartCFrame(GetMousePlacementCFrame(Plot, Object)) is what I have the objects move but they don’t stay together with my model’s primary part. Only the primary part changes orientation the model does not

1 Like

In order to solve this, we would have to see the rest of your code. Are you getting any errors in the output?

1 Like

But I also want to change the orientation

1 Like

local Player = game.Players.LocalPlayer

local Mouse = Player:GetMouse()

local Camera = game.Workspace.CurrentCamera

local BuildPro = false

local OnPlot = false

local BuildUi = script.Parent.FHOS.Frame.BuildFrame

local MarketPlaceService = game:GetService(“MarketplaceService”)

local RunService = game:GetService(“RunService”)

game.Players.PlayerAdded:Connect(function()

local hasPass = false

local success, message = pcall(function()

hasPass = MarketPlaceService:UserOwnsGamePassAsync(Player.UserId, 11321181)

end)

if not success then

warn("Error while checking if player has pass: " … tostring(message))

return

end

if hasPass == true then

BuildPro = true

end

end)

local UIS = game:GetService(“UserInputService”)

local Grid = 2

local Rot = 0

local PosX

local PosY

local PosZ

local Plot

local plotName

local PlotO

local PlotX

local PlotY

local PlotZ

local Object

local GivenPlot = game.ReplicatedStorage.BuildEvents.GivenPlot

local PlaceObject = game.ReplicatedStorage.BuildEvents.PlaceObject

GivenPlot.OnClientEvent:Connect(function(plt)

Plot = plt

plotName = plt.Name

PlotO = plt.Orientation

PlotX = PlotO.X

PlotY = PlotO.Y

PlotZ = PlotO.Z

end)

Plot.Touched:Connect(function(hit)

local plr = game:GetService(“Players”):GetPlayerFromCharacter(hit.Parent)

if plr then

OnPlot = true

end

end)

local CanStart = true

local Placing = nil

local CanPlace = nil

local MoveObject = false

local CanPlace

local BuildMode = Instance.new(“BoolValue”)

BuildMode.Parent = Player

BuildMode = false

BuildSelection = BuildUi.ScrollingFrame

Soil = BuildSelection.SoilSelect.ImageButton

StartBarn = BuildSelection.StartBarnSelect.ImageButton

Tent = BuildSelection.TentSelect.ImageButton

HayBale = BuildSelection.HayBaleSelect.ImageButton

Log = BuildSelection.LogSelect.ImageButton

GreenScreen = BuildSelection.GreenScreenSelect.ImageButton

Flag = BuildSelection.FlagSelect.ImageButton

SmallDeck = BuildSelection.SmallDeck.ImageButton

DeckStairs = BuildSelection.DeckStairs.ImageButton

LargeDeck = BuildSelection.LargeDeck.ImageButton

Chair = BuildSelection.Chair.ImageButton

Bed = BuildSelection.Bed.ImageButton

TallBillboard = BuildSelection.TallBillboard.ImageButton

ShortBillboard = BuildSelection.ShortBillboard.ImageButton

Table = BuildSelection.Table.ImageButton

CampFire = BuildSelection.CampFire.ImageButton

script.Parent.FHOS.Frame.Frame.BuildModeApp.TextButton.MouseButton1Click:Connect(function()

script.Parent.FHOS.Frame.Frame.Visible = false

script.Parent.FHOS.Frame.FhBrand.Visible = false

script.Parent.FHOS.Frame.Power.Visible = false

script.Parent.FHOS.Frame.Time.Visible = false

script.Parent.FHOS.Frame.Network.Visible = false

script.Parent.FHOS.Frame.Battery.Visible = false

script.Parent.FHOS:TweenPosition(UDim2.new(0.105, 0,0.485, 0), “Out”, “Linear”, 1, false, nil)

while script.Parent.FHOS.Rotation > -90 do

script.Parent.FHOS.Rotation = script.Parent.FHOS.Rotation - 1

wait()

end

BuildMode = true

if BuildMode == true and OnPlot == true or BuildPro == true then

Camera.CameraSubject = Plot

end

end)

local function Round(n, to)

local to = to or 1

if to == 0 then return n end

return math.floor(n/to + 0.5) * to

end

local function SnapToGrid(vector3)

return Vector3.new(

Round(vector3.X, Grid),

Round(vector3.Y, 0),

Round(vector3.Z, Grid)

)

end

local function CloneService(Obj)

Object = game.ReplicatedStorage.BuildObjects:FindFirstChild(Obj):Clone()

Object.Parent = game.Workspace

Mouse.TargetFilter = Object

Object.PrimaryPart.Orientation = Vector3.new(0, PlotY, 0)

end

Soil.MouseButton1Click:Connect(function()

CloneService(“Soil”)

MoveObject = true

end)

StartBarn.MouseButton1Click:Connect(function()

CloneService(“StarterBarn”)

MoveObject = true

end)

Tent.MouseButton1Click:Connect(function()

CloneService(“Tent”)

MoveObject = true

end)

HayBale.MouseButton1Click:Connect(function()

CloneService(“Haybale”)

MoveObject = true

end)

Log.MouseButton1Click:Connect(function()

CloneService(“Log”)

MoveObject = true

end)

GreenScreen.MouseButton1Click:Connect(function()

CloneService(“GreenScreen”)

MoveObject = true

end)

Flag.MouseButton1Click:Connect(function()

CloneService(“Flag”)

MoveObject = true

end)

SmallDeck.MouseButton1Click:Connect(function()

CloneService(“SmallDeck”)

MoveObject = true

end)

DeckStairs.MouseButton1Click:Connect(function()

CloneService(“DeckStairs”)

MoveObject = true

end)

LargeDeck.MouseButton1Click:Connect(function()

CloneService(“LargeDeck”)

MoveObject = true

end)

Chair.MouseButton1Click:Connect(function()

CloneService(“Chair”)

MoveObject = true

end)

Bed.MouseButton1Click:Connect(function()

CloneService(“Bed”)

MoveObject = true

end)

TallBillboard.MouseButton1Click:Connect(function()

CloneService(“TallBillboard”)

MoveObject = true

end)

ShortBillboard.MouseButton1Click:Connect(function()

CloneService(“ShortBillboard”)

MoveObject = true

end)

Table.MouseButton1Click:Connect(function()

CloneService(“Table”)

MoveObject = true

end)

CampFire.MouseButton1Click:Connect(function()

CloneService(“Campfire”)

MoveObject = true

end)

local function GetMousePlacementCFrame(Plot, Object)

local objectOffset = Vector3.new(0, Object.PrimaryPart.Size.Y/2, 0)

local mouseHit = Mouse.Hit

local mouseHitRelative = Plot.CFrame:ToObjectSpace(mouseHit) + objectOffset

local mouseHitRelativeSnapped = CFrame.new( SnapToGrid(mouseHitRelative.p))

local mouseHitWorldSnapped = Plot.CFrame * mouseHitRelativeSnapped

return mouseHitWorldSnapped

end

local function updateModelPlacement()

if Plot and (not Plot:IsDescendantOf(Object)) and Mouse.Target.Name == plotName then

Object:SetPrimaryPartCFrame(GetMousePlacementCFrame(Plot, Object))

end

end

–TAKE THIS PART OUT FOR RELEASE

BuildMode = true

RunService.RenderStepped:Connect(function()

Mouse.TargetFilter = Object

if BuildMode == true and MoveObject == true then

Plot.Texture.Transparency = 0

updateModelPlacement()

end

end)

1 Like

Set the CFrame instead to do this you Just gotta do

Model.PrimaryPart.CFrame = CFrame.new(1,1,1)
1 Like

Can you possibly wrap your code in ``` so that it is more readable? Also, are you getting any errors? If so, what line, and what error?

1 Like
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Camera = game.Workspace.CurrentCamera

local BuildPro = false
local OnPlot = false

local BuildUi = script.Parent.FHOS.Frame.BuildFrame

local MarketPlaceService = game:GetService("MarketplaceService")
local RunService = game:GetService("RunService")
game.Players.PlayerAdded:Connect(function()
	local hasPass = false
	
	local success, message = pcall(function()
		hasPass = MarketPlaceService:UserOwnsGamePassAsync(Player.UserId, 11321181)
	end)
	if not success then
		warn("Error while checking if player has pass: " .. tostring(message))
		return
	end
	if hasPass == true then
		BuildPro = true
	end
end)

local UIS = game:GetService("UserInputService")
local Grid = 2
local Rot = 0

local PosX
local PosY
local PosZ
local Plot
local plotName
local PlotO
local PlotX
local PlotY
local PlotZ
local Object

local GivenPlot = game.ReplicatedStorage.BuildEvents.GivenPlot
local PlaceObject = game.ReplicatedStorage.BuildEvents.PlaceObject
GivenPlot.OnClientEvent:Connect(function(plt)
	Plot = plt
	plotName = plt.Name
	PlotO = plt.Orientation
	PlotX = PlotO.X
	PlotY = PlotO.Y
	PlotZ = PlotO.Z
end)

Plot.Touched:Connect(function(hit)
	local plr = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if plr then
		OnPlot = true
	end
end)

local CanStart = true
local Placing = nil
local CanPlace = nil
local MoveObject = false
local CanPlace
local BuildMode = Instance.new("BoolValue")
BuildMode.Parent = Player
BuildMode = false
BuildSelection = BuildUi.ScrollingFrame
Soil = BuildSelection.SoilSelect.ImageButton
StartBarn = BuildSelection.StartBarnSelect.ImageButton
Tent = BuildSelection.TentSelect.ImageButton
HayBale = BuildSelection.HayBaleSelect.ImageButton
Log = BuildSelection.LogSelect.ImageButton
GreenScreen = BuildSelection.GreenScreenSelect.ImageButton
Flag = BuildSelection.FlagSelect.ImageButton
SmallDeck = BuildSelection.SmallDeck.ImageButton
DeckStairs = BuildSelection.DeckStairs.ImageButton
LargeDeck = BuildSelection.LargeDeck.ImageButton
Chair = BuildSelection.Chair.ImageButton
Bed = BuildSelection.Bed.ImageButton
TallBillboard = BuildSelection.TallBillboard.ImageButton
ShortBillboard = BuildSelection.ShortBillboard.ImageButton
Table = BuildSelection.Table.ImageButton
CampFire = BuildSelection.CampFire.ImageButton

script.Parent.FHOS.Frame.Frame.BuildModeApp.TextButton.MouseButton1Click:Connect(function()
	script.Parent.FHOS.Frame.Frame.Visible = false
	script.Parent.FHOS.Frame.FhBrand.Visible = false
	script.Parent.FHOS.Frame.Power.Visible = false
	script.Parent.FHOS.Frame.Time.Visible = false
	script.Parent.FHOS.Frame.Network.Visible = false
	script.Parent.FHOS.Frame.Battery.Visible = false
	script.Parent.FHOS:TweenPosition(UDim2.new(0.105, 0,0.485, 0), "Out", "Linear", 1, false, nil)
	while script.Parent.FHOS.Rotation > -90 do
		script.Parent.FHOS.Rotation = script.Parent.FHOS.Rotation - 1
		wait()
	end
	BuildMode = true
	if BuildMode == true and OnPlot == true or BuildPro == true then
		Camera.CameraSubject = Plot
	end
end)

local function Round(n, to)
	local to = to or 1
	if to == 0 then return n end
	return math.floor(n/to + 0.5) * to
end

local function SnapToGrid(vector3)
	return Vector3.new(
		Round(vector3.X, Grid),
		Round(vector3.Y, 0),
		Round(vector3.Z, Grid)
	)
end

local function CloneService(Obj)
	Object = game.ReplicatedStorage.BuildObjects:FindFirstChild(Obj):Clone()
	Object.Parent = game.Workspace
	Mouse.TargetFilter = Object
	
	Object.PrimaryPart.Orientation = Vector3.new(0, PlotY, 0)
end

Soil.MouseButton1Click:Connect(function()
	CloneService("Soil")
	MoveObject = true
end)
StartBarn.MouseButton1Click:Connect(function()
	CloneService("StarterBarn")
	MoveObject = true
end)
Tent.MouseButton1Click:Connect(function()
	CloneService("Tent")
	MoveObject = true
end)
HayBale.MouseButton1Click:Connect(function()
	CloneService("Haybale")
	MoveObject = true
end)
Log.MouseButton1Click:Connect(function()
	CloneService("Log")
	MoveObject = true
end)
GreenScreen.MouseButton1Click:Connect(function()
	CloneService("GreenScreen")
	MoveObject = true
end)
Flag.MouseButton1Click:Connect(function()
	CloneService("Flag")
	MoveObject = true
end)
SmallDeck.MouseButton1Click:Connect(function()
	CloneService("SmallDeck")
	MoveObject = true
end)
DeckStairs.MouseButton1Click:Connect(function()
	CloneService("DeckStairs")
	MoveObject = true
end)
LargeDeck.MouseButton1Click:Connect(function()
	CloneService("LargeDeck")
	MoveObject = true
end)
Chair.MouseButton1Click:Connect(function()
	CloneService("Chair")
	MoveObject = true
end)
Bed.MouseButton1Click:Connect(function()
	CloneService("Bed")
	MoveObject = true
end)
TallBillboard.MouseButton1Click:Connect(function()
	CloneService("TallBillboard")
	MoveObject = true
end)
ShortBillboard.MouseButton1Click:Connect(function()
	CloneService("ShortBillboard")
	MoveObject = true
end)
Table.MouseButton1Click:Connect(function()
	CloneService("Table")
	MoveObject = true
end)
CampFire.MouseButton1Click:Connect(function()
	CloneService("Campfire")
	MoveObject = true
end)
local function GetMousePlacementCFrame(Plot, Object)
	local objectOffset = Vector3.new(0, Object.PrimaryPart.Size.Y/2, 0)
	
	local mouseHit = Mouse.Hit
	
	local mouseHitRelative = Plot.CFrame:ToObjectSpace(mouseHit) + objectOffset
	
	local mouseHitRelativeSnapped = CFrame.new( SnapToGrid(mouseHitRelative.p))
	
	local mouseHitWorldSnapped = Plot.CFrame * mouseHitRelativeSnapped
	
	return mouseHitWorldSnapped
end

local function updateModelPlacement()
	if Plot and (not Plot:IsDescendantOf(Object)) and Mouse.Target.Name == plotName then
		Object:SetPrimaryPartCFrame(GetMousePlacementCFrame(Plot, Object))
	end
end

--TAKE THIS PART OUT FOR RELEASE
BuildMode = true

RunService.RenderStepped:Connect(function()
	Mouse.TargetFilter = Object
	if BuildMode == true and MoveObject == true then
		Plot.Texture.Transparency = 0
		updateModelPlacement()
	end
end)

I get no errors because there isn’t an error it’s just a orientation problem and position problem. I want the whole model to move and have the plots orientation but the model doesn’t orient the rest of the model with the primary part

1 Like

You can try and union all the parts in the model if possible.

1 Like

To rotate

Model.PrimaryPart.Orientation = CFrame.Angles(math.rad(1),math.rad(1),math.rad(1))

I dont know if this would work but try it atleast

1 Like

I try that but when the part contains cylinders it makes the cylinders orientation change making the cylinders on their round side not the flat side

1 Like
model:SetPrimaryPartCFrame(CFrame.new(position)*CFrame.Angles(math.rad(90),0,0))

This should work if you have a primary part in your model (aka the part that allows the model to rotate / move around)

1 Like

Put the orientation matrix inside the SetPrimaryPartCFrame argument, so that the other parts get rotated as well, as demonstrated by @greatneil80.

1 Like

You can loop through all the parts in the model and just change their Orientation, example:

local model = script.Parent;

for _, v in ipairs(model:GetDescendants()) do
	
	if v:IsA("Part") or v:IsA("MeshPart") or v:IsA("UnionOperation") then
		
		v.CFrame = CFrame.new(v.CFrame.Position) * CFrame.Angles(math.rad(25), math.rad(25), math.rad(25))
	end
end
1 Like

You are multiplying the cframe and the orientation and you shouldnt multiply it

1 Like