Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to make a realistic and smooth elevator using PrimaryPartCFrame?

Asked by 1 year ago
Edited 1 year 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 answer

Log in to vote
1
Answered by
NEILASc 84
1 year ago

the lerp function only takes 2 inputs. one of them is time (0 to 1) and the end position or cframe or value of anything.

example: your start position is 0,0,0 and end position is 0,10,0. and you use the lerp function and the time is set to 0.5. the current position would be 0,5,0

and i reccommend using the api service about tweenservice.

0
thx T3_MasterGamer 2189 — 1y
Ad

Answer this question