Trying to set just the Y value of the primarypart cframe

I’d like to know the best way to set the Y value of a model CFrame, seems easy but my brain isn’t working today & the game setup makes this difficult. (Appreciate any help)

Just need to set the Y value of a CFrame and nothing else (Not something like this)

self.Base:SetPrimaryPartCFrame(CFrame.new(0, sealevl + 0.5, )

I just want to set the Y Value while changing nothing else.

I’ve tried looking through the developer hub, but most solutions have depreciated vars or changed the X, Y & Z

Here is the current code I’m working with
(This is somewhat using OOP, I’m using the self variable, the way the game is formatted and the scripts I’m working with won’t allow the simple: CFrame.Y = or Position.Y = )

local Structure = require(game.ReplicatedStorage.Classes.Structures.Structure)
local Dock = Structure:Extend(script.Name)
local sealevel = game.ReplicatedStorage.Constants.OceanSurfaceY.Value

function Dock:OnNew()
	self.PrimaryPart = self.Base.PrimaryPart
	
	if self.PrimaryPart then
		print(self.PrimaryPart.Name)
		if self.PrimaryPart.Name == "Wood" then
		self.Base:SetPrimaryPartCFrame(CFrame.new(self.PrimaryPart.CFrame, sealevel + 0.5 ,self.PrimaryPart.CFrame.Z) * self.Base.PrimaryPart.CFrame:toOrientation() ) 
		end
	end
end


return Dock

Thanks for any help in advance!

local function scf(cframe, xyz)
	if xyz.x ~= 0 then
		return CFrame.new(xyz.x, cframe.y, cframe.z)
	end
	if xyz.y ~= 0 then
		return CFrame.new(cframe.x, xyz.y, cframe.z)
	end
	if xyz.z ~= 0 then
		return CFrame.new(cframe.x, cframe.y, xyz.z)
	end
end

part.CFrame = scf(part.CFrame, Vector3.new(0, 5, 0))
1 Like

Wouldn’t work sorry, remember it’s using the self var.

you should be able to use self.Base.CFrame = scf(self.Base.CFrame, Vector3.new(0, newY, 0)) no?

or self.Base:SetPrimaryPartCFrame(scf(self.Base.CFrame, Vector3.new(0, newY, 0)))

Your right just had to format it I’ll test it now:

local Structure = require(game.ReplicatedStorage.Classes.Structures.Structure)
local Dock = Structure:Extend(script.Name)
local sealevel = game.ReplicatedStorage.Constants.OceanSurfaceY.Value

function Dock:OnNew()
	self.PrimaryPart = self.Base.PrimaryPart
	
	if self.PrimaryPart then
		print(self.PrimaryPart.Name)
		if self.PrimaryPart.Name == "Wood" then
		---self.Base:SetPrimaryPartCFrame(CFrame.new(self.PrimaryPart.CFrame, sealevel + 0.5 ,self.PrimaryPart.CFrame.Z) * self.Base.PrimaryPart.CFrame:toOrientation() ) 
			print("Is Past")
			self.PrimaryPart = Dock:scf(self.PrimaryPart.CFrame, Vector3.new(0, 5, 0))
		end
	end
end
function Dock:scf(cframe, xyz)
	if xyz.x ~= 0 then
		return CFrame.new(xyz.x, cframe.y, cframe.z)
	end
	if xyz.y ~= 0 then
		return CFrame.new(cframe.x, xyz.y, cframe.z)
	end
	if xyz.z ~= 0 then
		return CFrame.new(cframe.x, cframe.y, xyz.z)
	end
end



return Dock

.CFrame?


Mb, I missed that lel thx for catching it.

Hi!

If your use-case is to move the model, then PivotTo is the way to go. :slight_smile:

PivotTo is the new more performant way of moving objects.

local YMovement = 5
Model:PivotTo(Model:GetPivot() + Vector3.new(0,YMovement,0))
2 Likes

There is a problem with that, so you see basically this is a placement, I can’t use the old self.PrimaryPart.CFrame.X as the reference is different due to this being global it grabs the model in Replicated storage, so it would set the X to however the old one is, which isn’t ideal because you are supposed to be placing the model. (Prob should’ve clarified that)

I’ll show you a video, essentially I need it to be on the water after placement.

As I’m testing to record a video, just realized this works!!!

1 Like

I have to test one more time to make sure it’s working by anchoring it so ik it’s not just floating up.

Please go with using PivotTo over setting the primarypart’s CFrame. :slight_smile:

CFrame fits the code format more than I’d say Pivot and it’s what my manager wants me to use.

Never mind :frowning: it’s not working.

I disagree and will stand by my point that PivotTo is the way to go. But either way, happy scripting. Hope you find the workaround you’re looking for. :slight_smile:

At this point whichever works, I’m going to use it since I’ve been doing this for a few hours to no avail.

Why are you having such a hard time, it seems simple

take the Mouse.Hit, change the Y

send a video

1 Like
local currentCFrame = self.Base:GetPivot()
local yOffset = sealevel + 0.5 - currentCFrame.Position.Y
local newCFrame = currentCFrame + vector3.new(0, yOffset, 0)
self.Base:PivotTo(newCFrame)

or

local currentCFrame = self.Base:GetPivot()
local position = Vector3.new(currentCFrame.Position.X, sealevel + 0.5, currentCFrame.Position.Z)
local newCFrame = currentCFrame.Rotation + position
self.Base:PivotTo(newCFrame)
1 Like


I can tell it’s not working because it’s floating up, the setup is difficult and this is a problem because it’s going to be anchored.