1

# How to make a realistic and smooth elevator using PrimaryPartCFrame?

Edited 11 months ago

Hello! I'm trying to make a moving elevator using for my game (both lobby and game) and I found this script in a question. I tried it myself and it did work, but when I test it out I keep glitching out of the map, the elevator is glitching, going up and down for no reason at all, and randomly teleports up and down while moving. I'm finding a solution, or better, a different code that will look good in my work. Here's the code:

Server:

local TweenService = game:GetService("TweenService")

game.ReplicatedStorage.GoUp.OnServerEvent:Connect(function()
local Time = time()
local Total = 5
local Model = workspace.ElevatorWorking
local Start = Model:GetPrimaryPartCFrame()
local End = workspace.clone1.B.CFrame
while time() - Time <= Total do
local TI = TweenService:GetValue((time() - Time)/Total, Enum.EasingStyle.Quad, Enum.EasingDirection.In)
local CFrame2 = Start:Lerp(End,TI)
Model:SetPrimaryPartCFrame(CFrame2)
wait()
end
end)

game.ReplicatedStorage.GoUp.OnServerEvent:Connect(function()
local Time = time()
local Total = 5
local Model = workspace.ElevatorWorking
local Start = Model:GetPrimaryPartCFrame()
local End = workspace.clone1.A.CFrame
while time() - Time <= Total do
local TI = TweenService:GetValue((time() - Time)/Total, Enum.EasingStyle.Quad, Enum.EasingDirection.In)
local CFrame2 = Start:Lerp(End,TI)
Model:SetPrimaryPartCFrame(CFrame2)
wait()
end
end)


Client:

local btnA = script.Parent.A
local btnB = script.Parent.B

btnA.MouseButton1Up:Connect(function()
game.ReplicatedStorage.GoDown:FireServer()
end)
btnB.MouseButton1Up:Connect(function()
game.ReplicatedStorage.GoUp:FireServer()
end)


(A is floor 1 while B is floor 2)

Thank you!

1