Help With C-Frames

  1. Help with elevator movement Keep it simple and clear!

  2. I can’t get my elevator to move smoothly

  3. I’ve already tried everything I know and all the resources I have knowledge of I tried to look for solutions on developer hub too, but no luck.

This is the script I’m using, everything works except for the movement

local Elevator = script.Parent.Parent  -- The Elevator model
local ElevatorControl = Elevator:FindFirstChild("ElevatorControl")
local IndoorButtons = script.Parent

-- Ensure ElevatorControl exists
if not ElevatorControl then
	warn("ElevatorControl RemoteEvent not found!")
	return
end

-- Define the floor positions
local floorPositions = {
	[1] = Vector3.new(20.785, 1, -30.746),  -- Ground floor
	[2] = Vector3.new(20.785, 25, -30.746), -- 1st floor
	[3] = Vector3.new(20.785, 50, -30.746), -- 2nd floor
	[4] = Vector3.new(20.785, 75, -30.746), -- 3rd floor
	[5] = Vector3.new(20.785, 100, -30.746), -- 4th floor
	[6] = Vector3.new(20.785, 150, -30.746)  -- 5th floor
}

-- Function to move the elevator to the target floor
local function moveToFloor(floorNumber)
	local targetPosition = floorPositions[floorNumber]
	if targetPosition then
		local currentPivot = Elevator:GetPivot()
		local targetCFrame = CFrame.new(targetPosition)
		Elevator:PivotTo(targetCFrame)
		--make sure the movement is smooth to the new position and doesnt teleport the elevator
	else
		warn("Invalid floor number: " .. tostring(floorNumber))
	end
end

-- Connect ClickDetectors to the buttons
for i, button in IndoorButtons:GetChildren() do
	if button:IsA("MeshPart") then
		local clickDetector = button:FindFirstChildOfClass("ClickDetector")
		if clickDetector then
			clickDetector.MouseClick:Connect(function()
				moveToFloor(tonumber(button.Name))
			end)
		end
	end
end

2 Likes

have you tried tweening?

4 Likes

Can You Help Me With That, I know that might be the solution, but I don’t script, can you show me how?

1 Like

there are a few code samples in the documentation, you could read those, but heres a simple way you could tween the elevator

at the top of your script, reference the tweenservice by doing local TweenService = game:GetService("TweenService")

then, make the tween information (this will be the duration of the animation and how it tweens (easing style): heres documentation for the different types of easing/smoothing styles if you want to read it EasingStyle | Documentation - Roblox Creator Hub
local info = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)

syntax: TweenInfo.new(Duration, Enum.EasingStyle, Enum.EasingDirection)

then, replace your pivot code with this to start the animation.
(i’m assuming that your elevator model is anchored and doesn’t have welds, so i’ll use the roundabout method to tween model positions)

local temp = Instance.new("CFrameValue") -- make a temporary value to store the tween variable
temp.Value = Elevator:GetPivot() -- set the initial position
local tween = TweenService:Create(temp, info, {Value = CFrame.new(targetPosition)}) -- creates the animation for the value
tween:Play()
local c
c = temp.Changed:Connect(function(value) -- every time the value is animated by the tween, move the elevator to the new position
    Elevator:PivotTo(value)
end)

tween.Completed:Connect(function() temp:Destroy() c:Disconnect() end)
3 Likes

let me know if this works!

(ignore this needed to add enough characters)

2 Likes

Ok so, I tried this script -

local Elevator = script.Parent.Parent  -- The Elevator model
local ElevatorControl = Elevator:FindFirstChild("ElevatorControl")
local IndoorButtons = script.Parent
local TweenService = game:GetService("TweenService")
local info = TweenInfo.new(1, Enum.EasingStyle.Circular, Enum.EasingDirection.InOut)


-- Ensure ElevatorControl exists
if not ElevatorControl then
	warn("ElevatorControl RemoteEvent not found!")
	return
end

-- Define the floor positions
local floorPositions = {
	[1] = Vector3.new(20.785, 1, -30.746),  -- Ground floor
	[2] = Vector3.new(20.785, 25, -30.746), -- 1st floor
	[3] = Vector3.new(20.785, 50, -30.746), -- 2nd floor
	[4] = Vector3.new(20.785, 75, -30.746), -- 3rd floor
	[5] = Vector3.new(20.785, 100, -30.746), -- 4th floor
	[6] = Vector3.new(20.785, 150, -30.746)  -- 5th floor
}

-- Function to move the elevator to the target floor
local function moveToFloor(floorNumber)
	local targetPosition = floorPositions[floorNumber]
	local temp = Instance.new("CFrameValue") -- make a temporary value to store the tween variable
	temp.Value = Elevator:GetPivot() -- set the initial position
	local tween = TweenService:Create(temp, info, {Value = CFrame.new(targetPosition)}) -- creates the animation for the value
	tween:Play()
	local c
	c = temp.Changed:Connect(function(value) -- every time the value is animated by the tween, move the elevator to the new position
		Elevator:PivotTo(value)
	end)

	tween.Completed:Connect(function() temp:Destroy() c:Disconnect() end)
end

Now for some reason, when the buttons are clicked the elevator doesn’t move.

Also Thanks for taking the time to help.

1 Like

are u changing the CFrame on the server or the client?
if its on the server then it willnot be smooth bc of network delay instead make it on the client

1 Like

You can animate your elevator using two ways

  1. Lerping
  2. Tweening

I would recommend tweening cause lerp uses more perfomance AFAIK

if you want to tween a model i would recommend you this guide from roblox code samples: https://create.roblox.com/store/asset/13595097154/Using-TweenService-on-a-Model

in short you need to weld all your model’s part to model primaryPart and after this you need to tween the CFrame of the primary part to the end point CFrame

If you want to tween it using a TweenService just create a new tween where you will move elevator’s primary part CFrame to the end point to tween the full model. it takes time but it works perfectly if elevator is a Model. hope it is a model lol

Edit: agree to what GE_0E said, it’s better to tween on a client so it will be smoother

ah i think i forgot to set the temp instance’s parent to the workspace. add temp.Parent = workspace below local temp = Instance.new("CFrameValue") and see if it works then

2 Likes

Still Didn’t Work for some reason :frowning:


If This Helps This Is the Explorer tab of the Elevator.
Note: Elevator Booth Is in the workspace.

1 Like

Off the top of my head, you should also be able to make this w/ Physics constraints but, I don’t know what your intended implementation is. If it’s something multiple players can get in and should be server-sided, physics constraints might be the way to go. AlignPosition w/ AlignOrientation(to keep it’s orientation locked) should be fairly easy to implement. You’d just have to make an attachment-based setup for each floor and an aligning attachment in the elevator that’ll cause it to move to said points.

1 Like

Lol Sorry but I barely Understand Scripting.

1 Like

by the way, this is a good idea, I don’t know why I didn’t mention it :thinking:

just want to ask you, @Project_Aurora , what do you think would be better: client sided tween animation or alignPosition from server? i think client-sided tween would be better the for game performance since server won’t need to handle physics using alignPosition, BUT with alignPosition it will look more naturally i guess and probably(im not sure) will be more secure.

just saw you got a nice profile on devforum so i think you are good at it :thinking:

1 Like

It depends, I haven’t really tested this myself but, I do know for a fact that Physics is updated on the server at 240hz. Though, I think you have to manually set that. Overall, physics constraints are usually the way to go due to that sole reason alone especially since mobile devices are sometimes <30 fps.

1 Like

The prismatic constraint in the image I provided, can that be used?

1 Like

No, what you would need is AlignPosition and AlignOrientation. AlignPosition would align ur elevator’s attachment to the floors level attachment if that makes sense. Are you familiar w/ physics constraints by chance?

No, wait are you saying instead of having vectors, I could instead use attachments?

Yes, exactly! Basically each floor will have an attachment that you would set your AlignPosition.Attachment1 to.

1 Like

I forgot to mention but, you can actually test this w/ 0 scripting. Just have a test platform with an attachment and another part with an attachment and set it up in studio just so u can adjust ur AlignPosition’s properties for the desired effect. Also recommend setting up the AlignOrientation while ur at it too.

Screenshot 2024-09-13 at 7.14.49 PM
is this good?