Hi, I am trying to make an elevator with multi-level functionality. The purpose of this is to allow players to travel floors or sublevels. The current issue with this is that there are controls. Inside and outside of the elevator. The code that I will provide below is the structure of how it will respond. Now this is given that if a new SubLevel was hypothetically created. It will add another floor from the script provided below. I need help to figure out how to create it to make it so that when a player is on floor 3 and calls the elevator which is on floor 1 it skips adjacent floors and continues going up to the floor it was called or vice versa. I also need some help with lastly the functionality of if it was called on a specific floor the sublevel doors to that sublevel open including with the cabin, and close when going up or down. I’m a beginner to scripting, this particular thing requires a lot of logical thinking at least in my opinion. Could I get some help with some solutions?
!
local ElevatorModule = script.Parent
--< Services >--
local RepStorage = game:GetService("ReplicatedStorage")
local DoorEvent = RepStorage.Events:WaitForChild("Door")
local TweenService = game:GetService("TweenService")
--< Constants >--
local Map = workspace:WaitForChild("Map")
local LCZ = Map.LCZ.Rooms
local ElevatorDuration = 5
local ElevatorTweenInfo = TweenInfo.new(ElevatorDuration, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
local ElevatorStudCount = 50
local DoorTween = TweenInfo.new(1.57, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
local DoorMoveCount = 1.5
local FLOOR_MIN = 1 -- The lowest SubLevel number
local FLOOR_MAX = 3 -- The highest SubLevel number
--< Initialize >--
local ElevatorActive = function (Elevator)
local ElevatorModel = Elevator
local SubLevelsFolder = ElevatorModel.Levels
local SubLevelInt = ElevatorModel.SubLevel
local ElevatorCabinModel = ElevatorModel.Cabin
local CabinIsOpen = ElevatorCabinModel.CabinIsOpen
local IsActive = ElevatorCabinModel.IsActive
local CabinDoor1 = ElevatorCabinModel.CabinDoor1
local CabinDoor2 = ElevatorCabinModel.CabinDoor2
local ElevatorCabinUpButtonPart = ElevatorCabinModel.UpButton
local UpButtonPrompt = ElevatorCabinUpButtonPart.Prompt
local ElevatorCabinDownButtonPart = ElevatorCabinModel.DownButton
local DownButtonPrompt = ElevatorCabinDownButtonPart.Prompt
local DoorOpen = function ()
TweenService:Create(CabinDoor1, DoorTween, {CFrame = CabinDoor1.CFrame * CFrame.new(DoorMoveCount, 0, 0)}):Play()
end
local DoorClose = function ()
TweenService:Create(CabinDoor1, DoorTween, {CFrame = CabinDoor1.CFrame * CFrame.new(-DoorMoveCount, 0, 0)}):Play()
end
local ElevatorMoveUp = function ()
for _, GetCabinParts in pairs(ElevatorCabinModel:GetDescendants()) do
if GetCabinParts:IsA("BasePart") then
TweenService:Create(GetCabinParts, ElevatorTweenInfo, {CFrame = GetCabinParts.CFrame * CFrame.new(0, ElevatorStudCount, 0)}):Play()
end
end
print("Going up")
task.wait(ElevatorDuration)
IsActive.Value = false
CabinIsOpen.Value = true
SubLevelInt.Value += 1
warn(SubLevelInt.Value)
end
local ElevatorMoveDown = function ()
for _, GetCabinParts in pairs(ElevatorCabinModel:GetDescendants()) do
if GetCabinParts:IsA("BasePart") then
TweenService:Create(GetCabinParts, ElevatorTweenInfo, {CFrame = GetCabinParts.CFrame * CFrame.new(0, -ElevatorStudCount, 0)}):Play()
end
end
print("Going down")
task.wait(ElevatorDuration)
IsActive.Value = false
SubLevelInt.Value -= 1
warn(SubLevelInt.Value)
end
local ElevatorMoveSpec = function (TargetFloor)
local Displacement = TargetFloor - SubLevelInt.Value
if Displacement ~= 0 then
local ElevatorTweenInfo = TweenInfo.new(ElevatorDuration * math.abs(Displacement), Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
for _, GetCabinParts in pairs(ElevatorCabinModel:GetDescendants()) do
if GetCabinParts:IsA("BasePart") then
TweenService:Create(GetCabinParts, ElevatorTweenInfo, {CFrame = GetCabinParts.CFrame * CFrame.new(0, ElevatorStudCount * Displacement, 0)}):Play()
end
end
print("Displacement:", Displacement, "\n(Positive = up, Negative = down)")
task.wait(ElevatorTweenInfo.Time)
CabinIsOpen.Value = true
SubLevelInt.Value = TargetFloor
end
end
local ElevatorSignal = function (MoveDirection, TargetLevel)
if IsActive.Value == false then
IsActive.Value = true
if MoveDirection == "Up" then
if SubLevelInt.Value < FLOOR_MAX then
ElevatorMoveUp()
if CabinIsOpen.Value == true then
if IsActive.Value == false then
DoorOpen()
end
end
else
warn("Already reached the highest floor!")
end
elseif MoveDirection == "Down" then
if SubLevelInt.Value > FLOOR_MIN then
ElevatorMoveDown()
else
warn("Already reached the lowest floor!")
end
elseif MoveDirection == "Floor" then
warn("Skipping to floor " .. tostring(TargetLevel) .. "..")
ElevatorMoveSpec(TargetLevel)
--[[
if SubLevelInt.Value < TargetLevel then
repeat ElevatorMoveUp() until SubLevelInt.Value == TargetLevel
elseif SubLevelInt.Value > TargetLevel then
repeat ElevatorMoveDown() until SubLevelInt.Value == TargetLevel
end
--]]
end
warn("Reached floor", SubLevelInt.Value, ".")
IsActive.Value = false
end
end
local SubLevelGet = function (SubLevel)
local SubLevelModel = SubLevel
local SubLevelInteger = tonumber(string.sub(SubLevelModel.Name, 10))
if not SubLevelInteger then
warn("This sublevel does not have a proper numeration! Ignoring..")
return
end
--< Values >--
local IsOpen = SubLevelModel.IsOpen
local Locked = SubLevelModel.Locked
local OuterDoorPart1 = SubLevelModel.OuterDoor1
local OuterDoorPart2 = SubLevelModel.OuterDoor2
local ButtonPart = SubLevelModel.Button
local ButtonPrompt = ButtonPart.Prompt
ButtonPrompt.Triggered:Connect(function()
ElevatorSignal("Floor", SubLevelInteger)
end)
end
for _, v in pairs(SubLevelsFolder:GetChildren()) do
if string.match(v.Name, "SubLevel") and v:IsA("Model") then
SubLevelGet(v)
v.ChildAdded:Connect(SubLevelGet)
end
end
UpButtonPrompt.Triggered:Connect(function()
if SubLevelInt.Value ~= 3 then
ElevatorSignal("Up")
end
end)
DownButtonPrompt.Triggered:Connect(function()
if SubLevelInt.Value ~= 1 then
ElevatorSignal("Down")
end
end)
end
--< ChildAdded Check >--
for _, v in pairs(LCZ:GetDescendants()) do
if v.Name == "ComplexElevator" and v:IsA("Model") then
ElevatorActive(v)
end
if v.Name == "Elevators" and v:IsA("Folder") then
v.ChildAdded:Connect(ElevatorActive)
end
end
return ElevatorModule