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