Diagonal Elevator Physics Issues

Currently for a game, I’m trying to make a diagonal elevator. I’m having issues with… lots of things. Namely:
1: Players sliding off of the elevator randomly and it being generally unreliable for sticking players to it.
2: Inconsistent timing with the tween
3: A lovely plethora of other bugs.

Any help would be absolutely lifesaving. I’ve been stuck with this for days.
The library is as follows:

local ElevatorLib = {}

local RunService = game:GetService("RunService")
local SoundLibrary = require(game:GetService("ServerScriptService").Libraries:FindFirstChild("SoundLib"))
local TweenService = game:GetService("TweenService")

ElevatorLib.__index = ElevatorLib

local movesound

local function UnanchorModel(model)
	for _, i in ipairs(model:GetDescendants()) do
		if i:IsA("BasePart") or i:IsA("UnionOperation") then
			i.Anchored = false
		end
	end
end


local function zeroModelVelocity(model)
	local rootParts = {}

	for _, part in ipairs(model:GetDescendants()) do
		if part:IsA("BasePart") then
			local root = part:GetRootPart()
			if root and not rootParts[root] then
				rootParts[root] = true
			end
		end
	end

	for root in pairs(rootParts) do
		task.spawn(function()
			root.Anchored = true
			local originalCFrame = root.CFrame
			root.Velocity = Vector3.zero
			root.RotVelocity = Vector3.zero
			root.AssemblyLinearVelocity = Vector3.zero
			root.AssemblyAngularVelocity = Vector3.zero

			for _ = 1, 3 do
				RunService.Heartbeat:Wait()
			end

			root.CFrame = originalCFrame
			root.Velocity = Vector3.zero
			root.RotVelocity = Vector3.zero
			root.AssemblyLinearVelocity = Vector3.zero
			root.AssemblyAngularVelocity = Vector3.zero
		end)
	end
end

local function getHumanoidsTouching(model)
	local found = {}
	for _, part in ipairs(model:GetDescendants()) do
		if part:IsA("BasePart") then
			local touchingParts = part:GetTouchingParts()
			for i = 1, #touchingParts do
				local touching = touchingParts[i]
				local char = touching:FindFirstAncestorOfClass("Model")
				if char and not found[char] then
					local humanoid = char:FindFirstChildOfClass("Humanoid")
					local hrp = char:FindFirstChild("HumanoidRootPart")
					if humanoid and hrp then
						found[char] = hrp
					end
				end
			end
		end
	end
	return found
end


function ElevatorLib.NewFloored(ElevatorPart: Instance, Floors, FloorTimes, OverrideDefaultFloor, Sounds)
	local self = setmetatable({}, ElevatorLib)

	self.CurrentFloor = OverrideDefaultFloor or 1

	if not Sounds then
		Sounds = {
			["startsoundid"] = "8624230673",
			["movesoundid"] = "18218672455",
			["stopsoundid"]  = "8624225809"
		}
	end

	if ElevatorPart:IsA("Model") then
		warn("Warning: Creating an elevator with a model will not work. Defaulting to PrimaryPart.")
		self.MovePart = ElevatorPart.PrimaryPart
	else
		self.MovePart = ElevatorPart
	end

	if typeof(Floors) ~= "table" or typeof(FloorTimes) ~= "table" then
		warn("ERROR: Failed to create elevator. Floors/FloorTimes must be tables.")
		return {}
	end

	self.Sounds      = Sounds
	self.Floors      = Floors
	self.FloorTimes  = FloorTimes
	self.Type        = "Floored"
	self.State       = "Not Moving"

	print("🟢 [Elevator Lib] Created elevator: " .. self.MovePart.Parent.Name)
	for i=1, #Floors do
		print(string.format("Floor %d: Time=%.2f, CFrame=%s", i, FloorTimes[i], tostring(Floors[i])))
	end

	return self
end


function ElevatorLib:GoToFloor(floorNumber)
	task.spawn(function()
		print(string.format("🟢 [Elevator Lib] %s called to floor %d", self.MovePart.Parent.Name, floorNumber))

		self.State = "Moving"
		self.MovePart.Anchored = false

		local startPos = self.MovePart.Position
		local targetCFrame = self.Floors[floorNumber]
		local travelTime = self.FloorTimes[floorNumber]
		local goalPos = targetCFrame.Position

		SoundLibrary.PlaySound({ soundId = self.Sounds.startsoundid, looped = false, parent = self.MovePart })
		movesound = SoundLibrary.PlaySound({ soundId = self.Sounds.movesoundid, looped = true, parent = self.MovePart })

		if self.connection then self.connection:Disconnect() end
		if self.AlignPosition then self.AlignPosition:Destroy() self.AlignPosition = nil end
		if self.AlignOrientation then self.AlignOrientation:Destroy() self.AlignOrientation = nil end

		local att = Instance.new("Attachment")
		att.Name = "ElevatorAttachment"
		att.Parent = self.MovePart

		local ap = Instance.new("AlignPosition")
		ap.Mode = Enum.PositionAlignmentMode.OneAttachment
		ap.RigidityEnabled = true
		ap.MaxForce = math.huge
		ap.Responsiveness = 75
		ap.ReactionForceEnabled = true
		ap.Attachment0 = att
		ap.Parent = self.MovePart

		local ao = Instance.new("AlignOrientation")
		ao.Mode = Enum.OrientationAlignmentMode.OneAttachment
		ao.RigidityEnabled = true
		ao.MaxTorque = math.huge
		ao.Responsiveness = 200
		ao.CFrame = targetCFrame - targetCFrame.Position
		ao.Attachment0 = att
		ao.Parent = self.MovePart

		self.AlignPosition = ap
		self.AlignOrientation = ao

		ap.Position = startPos

		local humanoids = getHumanoidsTouching(self.MovePart)

		for char, hrp in pairs(humanoids) do
			local humanoid = char:FindFirstChildOfClass("Humanoid")
			if humanoid then
				humanoid.PlatformStand = true
			end
		end

		local keepVelocityZero = RunService.Heartbeat:Connect(function()
			self.MovePart.Velocity = Vector3.new(0,0,0)
			self.MovePart.RotVelocity = Vector3.new(0,0,0)
			for char, hrp in pairs(humanoids) do
				if hrp and hrp.Parent then
					hrp.Velocity = Vector3.new(0,0,0)
					hrp.RotVelocity = Vector3.new(0,0,0)
				end
			end
		end)

		local tweenInfo = TweenInfo.new(travelTime, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
		local tweenGoal = { Position = goalPos }
		local tween = TweenService:Create(ap, tweenInfo, tweenGoal)

		tween:Play()
		ap.Position = goalPos
		tween.Completed:Wait()

		while (self.MovePart.Position - goalPos).magnitude > 0.5 do
			task.wait(0.1)
		end

		keepVelocityZero:Disconnect()

		for char, hrp in pairs(humanoids) do
			local humanoid = char:FindFirstChildOfClass("Humanoid")
			if humanoid then
				humanoid.PlatformStand = false
			end
			if hrp and hrp.Parent then
				hrp.Velocity = Vector3.new(0,0,0)
				hrp.RotVelocity = Vector3.new(0,0,0)
			end
		end

		if self.AlignPosition then self.AlignPosition:Destroy() self.AlignPosition = nil end
		if self.AlignOrientation then self.AlignOrientation:Destroy() self.AlignOrientation = nil end

		local existingAtt = self.MovePart:FindFirstChild("ElevatorAttachment")
		if existingAtt then existingAtt:Destroy() end

		self.MovePart.CFrame = targetCFrame
		self.State = "Not Moving"

		if movesound then movesound:Stop() end
		SoundLibrary.PlaySound({ soundId = self.Sounds.stopsoundid, looped = false, parent = self.MovePart })

		self.CurrentFloor = floorNumber
		print("🟢 [ElevatorLib] Arrived at floor " .. floorNumber .. ".")
		print("Velocities of players after arrival:")
		local humanoids = getHumanoidsTouching(self.MovePart)
		for char, hrp in pairs(humanoids) do
			print(hrp.Name, hrp.Velocity, hrp.AssemblyLinearVelocity)
		end
		self.MovePart.Anchored = true
			zeroModelVelocity(self.MovePart.Parent)
		task.wait(0.2)
		
	end)

	return self.FloorTimes[floorNumber]
end
return ElevatorLib

you could use prismatic constraints to keep the players sticking to it

I tried this, but I have absolutely zero idea how to actually make it work properly.

Hello!

I understand that creating a reliable elevator is a challenging task, especially when dealing with issues such as player slippage, inconsistent animations, and other errors. Here are some tips and approaches that can help you address these challenges:

1. Preventing players from slipping
Problem: Players sometimes slide off the elevator because their physical properties or interactions are not properly managed.

Decision:

  • Keep players “attached” to the elevator:
    – When the elevator is moving, you can temporarily spawn (or connect) their HumanoidRootPart to the elevator using AlignPosition or WeldConstraint.
    – Or set PlatformStand = true on the Humanoid to prevent the player from being controlled while moving.

  • Make sure that players cannot slip:
    – Before starting the movement, set Humanoid.PlatformStand = true, and then set it back to false.
    – You can also use WeldConstraint or AlignPosition to “stick” players to the elevator.

2. Animation sequence and movement
Problem: Animation or movement is not working consistently.

Decision:

  • Use TweenService for smooth movements:
    – Make sure that Tween is created and started once, and not repeatedly in a loop.
    – In your code, you are using TweenService correctly, just make sure that ap.Position and tween:Play() are called only once and synchronously.
  • Updating the position at the end of the animation:
    – Set ap.Position only after the Tween. has finished.
    – Do not manually change the position during the tween to avoid conflicts.

3. Secure retention of elevator and players

  • Use WeldConstraint or AlignPosition with RigidityEnabled = true to lock the elevator and players in place during movement.
  • Ensure that the elevator and players have the same base point (such as HumanoidRootPart) and that they are connected during movement.

4. General tips

  • Error handling and timings:
    – Use tween.Completed:Wait() to wait for the movement to complete.
    – After that, explicitly reset the states.
  • Interaction check:
    – When starting movement, disable physical interactions for players to avoid slipping.
    – After completion, return them to normal mode.

An example of how to “stick” a player to an elevator:

local function AttachPlayerToLift(player, lift)
    local hrp = player.Character:FindFirstChild("HumanoidRootPart")
    if hrp then
        -- Create WeldConstraint
        local weld = Instance.new("WeldConstraint")
        weld.Part0 = hrp
        weld.Part1 = lift
        weld.Parent = hrp
        -- We memorize the weld so that we can delete it later.
        player.Character:SetAttribute("LiftWeld", weld)
        -- Blocking the player's control:
        player.Character.Humanoid.PlatformStand = true
    end
end

local function DetachPlayerFromLift(player)
    local weld = player.Character:GetAttribute("LiftWeld")
    if weld and weld.Parent then
        weld:Destroy()
        player.Character:SetAttribute("LiftWeld", nil)
        player.Character.Humanoid.PlatformStand = false
    end
end

Call AttachPlayerToLift(player, self.MovePart) before starting the movement and DetachPlayerFromLift(player) after.

Result:

  • Use WeldConstraint or AlignPosition to fix the players and the elevator.
  • Ensure consistency in the animation and wait for the Tween to complete.
  • Temporarily disable player control to avoid slipping.
  • Run tests with different scenarios to identify bottlenecks.

If you wish, I can help you in more specific ways, such as providing an example of a complete player capture implementation or explaining how to implement a reliable movement mechanism.

i will try this, but ideally i’d like to allow for players to freely move.

upon looking at the constraints while testing the elevator, on the first going down, the constraint has a certain level of forwards drag, wherein the tween looks to be pulling it away from the elevator quicker than the elevator is moving. when it’s like this, players seem to stick on, but after the first stopping of the elevator, the elevator and constraint are always at the same position, and players tend to slip off.

Solved this by repurposing another forum script used for trains.