How do you move a model upwards with CFrame?

I am making a lift and need help moving it up and down. I used Position to move the primary part of the lift up and down when I press a certain button, but it did not move the lift components. So, I used CFrame and it worked. The problem is the lift rotates as it goes up. I think this is because of the CFrame LookVector. What look vector should I use so the lift does not spin whilst moving up and down?

Edit: Video of problem.
robloxapp-20211205-0709523.wmv (935.1 KB)

Edit: Main code.

local placeholderDoors = script.Parent.Parent:WaitForChild("Placeholder_Doors")
local isMoving = script.Parent.Is_Moving
local floorLevels = workspace:WaitForChild("Floor_Levels")
local currentFloor = script.Parent.Current_Floor
local buttons = script.Parent:WaitForChild("Buttons"):GetChildren()
local movingFloor = script.Parent.PrimaryPart

local tweenservice = game:GetService("TweenService")


for i, v in pairs(buttons) do
	local clickDetector = Instance.new("ClickDetector")
	clickDetector.Parent = v
	
	clickDetector.MouseClick:Connect(function(player)
		if isMoving.Value == true then
			--they must wait
		else
			local moveTime = math.sqrt((script.Parent.PrimaryPart.Position-player.Character.HumanoidRootPart.Position).Magnitude)
			print(moveTime)
			--tween the elevator to their position 
			local tweenInfo = TweenInfo.new(
				moveTime,
				Enum.EasingStyle.Linear
			)
			local floor = script.Parent.Moving_Floor
			print(floor)
			print(v.Name)
			local position = Vector3.new(floor.Position.X, floorLevels:WaitForChild(v.Name).Position.Y, floor.Position.Z)
			local vector = floor.CFrame.LookVector
			local tween = tweenservice:Create(floor, tweenInfo, {CFrame = CFrame.new(position, vector)})
			tween:Play()	
			
			isMoving.Value = true
			wait(moveTime)
			isMoving.Value = false
			
			local splitString = string.split(v.Name, "_")
			local number = splitString[2]
			currentFloor.Value = tonumber(number)
		end
	end)
end
1 Like

Code? ignore ignore

local isMoving = script.Parent.Is_Moving
local floorLevels = workspace:WaitForChild("Floor_Levels")
local currentFloor = script.Parent.Current_Floor
local buttons = script.Parent:WaitForChild("Buttons"):GetChildren()
local movingFloor = script.Parent.PrimaryPart

local tweenservice = game:GetService("TweenService")


for i, v in pairs(buttons) do
	local clickDetector = Instance.new("ClickDetector")
	clickDetector.Parent = v
	
	clickDetector.MouseClick:Connect(function(player)
		if isMoving.Value == true then
			--they must wait
		else
			local moveTime = math.sqrt((script.Parent.PrimaryPart.Position-player.Character.HumanoidRootPart.Position).Magnitude)
			print(moveTime)
			--tween the elevator to their position 
			local tweenInfo = TweenInfo.new(
				moveTime,
				Enum.EasingStyle.Linear
			)
			local floor = script.Parent.Moving_Floor
			print(floor)
			print(v.Name)
			local position = Vector3.new(floor.Position.X, floorLevels:WaitForChild(v.Name).Position.Y, floor.Position.Z)
			local vector = floor.CFrame.LookVector
			local tween = tweenservice:Create(floor, tweenInfo, {CFrame = CFrame.new(position, vector)})
			tween:Play()	
			
			isMoving.Value = true
			wait(moveTime)
			isMoving.Value = false
			
			local splitString = string.split(v.Name, "_")
			local number = splitString[2]
			currentFloor.Value = tonumber(number)
		end
	end)
end```

Roblox has a built-in called Prismatic Constraint. Maybe try tinkering with that?

use

part.CFrame *= CFrame.new(0,1,0)

for 1 stud

1 Like

If you only change the CFrame of the PrimaryPart directly, then only the primary part will move.

This is why Model:SetPrimaryPart() existed, but that has been deprecated in favor of Model:PivotTo()

Get the model (PrimaryPart’s parent model) and use

Model:PivotTo(Model:GetPivot() + Vector3.new(0,1,0))

to move the entire model up 1 stud.

Note: CFrame + Vector3.new(0,1,0) and CFrame * CFrame.new(0,1,0) do the same thing.

2 Likes