Recently I've been building a new HQ for my company. I am not a very good scripter, so I got an elevator off free models. I re-sized the elevator to fit the HQ but because of this, it takes ages to get from floor to floor. I tryed making it move faster but it breaks after a few push of the buttons. I'd appreciate it if someone took a look: http://www.roblox.com/Elevator-testing-S5S-HQ-item?id=149301346
Or here's the script:
e = script.Parent
car = e.Car
cfloor = 1
tfloor = 1
enabled = true
Closed = false
BellEn = true
initial = true
cardir = 1
queue = {}
function GetNextFloor()
if #queue == 0 then return nil end
table.sort(queue)
if cardir == 1 or cardir == 0 then
for x = 1, #queue do
if queue[x] > cfloor then
return queue[x] end
end
local closest = nil
for x1 = 1, #queue do
if queue[x1] < cfloor then
closest = queue[x1]
end
end
if closest ~= nil then
return closest end
elseif cardir == -1 then
local closest = nil
for x = 1, #queue do
if queue[x] < cfloor then
closest = queue[x]
end
end
if closest ~= nil then
return closest end
for x1 = 1, #queue do
if queue[x1] > cfloor then
return queue[x1] end
end
end
end
function FindIn(t, tar)
for a = 1, #t do
if t[a] == tar then
return a end
end
return nil
end
function CFrameMove(int)
local c = car:GetChildren()
local c1 = e.CarButtons:GetChildren()
local c2 = e.IndicatorLED:GetChildren()
for x = 1, #c do
if c[x]:findFirstChild("Reverse")~=nil then
c[x].CFrame = c[x].CFrame * CFrame.new(0,-int,0)
else
c[x].CFrame = c[x].CFrame * CFrame.new(0,int,0)
end
end
for x1 = 1, #c1 do
if c1[x1]:findFirstChild("Reverse")~=nil then
c1[x1].CFrame = c1[x1].CFrame * CFrame.new(0,-int,0)
else
c1[x1].CFrame = c1[x1].CFrame * CFrame.new(0,int,0)
end
end
for x2 = 1, #c2 do
if c2[x2].ClassName == "Part" then
if c2[x2]:findFirstChild("Reverse")~=nil then
c2[x2].CFrame = c2[x2].CFrame * CFrame.new(0,-int,0)
else
c2[x2].CFrame = c2[x2].CFrame * CFrame.new(0,int,0)
end
end
end
end
function Move(d)
if not enabled then return end
enabled = false
while Closed ~= true do
wait(0.1)
end
car.U.Texture.Texture = "http://www.roblox.com/asset/?id=67954815"
car.D.Texture.Texture = "http://www.roblox.com/asset/?id=67954815"
wait(0.5)
cardir = d
if d == 1 then
e.IndicatorLED.Display.Arw.Texture = "http://www.roblox.com/asset/?id="
else
e.IndicatorLED.Display.Arw.Texture = "http://www.roblox.com/asset/?id="
end
for i = 1, 10 do
CFrameMove(0.01*d)
wait(0.01)
end
for i1 = 1, 10 do
CFrameMove(0.03*d)
wait(0.01)
end
for i2 = 1, 10 do
CFrameMove(0.05*d)
wait(0.01)
end
for i3 = 1, 10 do
CFrameMove(0.07*d)
wait(0.01)
end
for i4 = 1, 10 do
CFrameMove(0.09*d)
wait(0.01)
end
for i5 = 1, 10 do
CFrameMove(0.1*d)
wait(0.01)
end
while true do
for x = 1, #e.Floors:GetChildren() do
if math.abs(e.Floors:GetChildren()[x].Position.y - car.Engine.Position.y) < 5 then
cfloor = tonumber(e.Floors:GetChildren()[x].Name)
e.IndicatorLED.Value.Value = tonumber(e.Floors:GetChildren()[x].Name)
end
end
if math.abs(e.Floors:findFirstChild(tfloor).Position.y - car.Engine.Position.y) < 0.02 then
break
elseif math.abs(e.Floors:findFirstChild(tfloor).Position.y - car.Engine.Position.y) < 1 then
CFrameMove(0.01*d)
e.CarButtons:findFirstChild("c"..tfloor).Button.Texture = "http://www.roblox.com/asset/?id=79508665"
e.HallButtons:findFirstChild("b"..tfloor).Button.Texture = "http://www.roblox.com/asset/?id=79508665"
e.IndicatorLED.Display.Arw.Texture = "http://www.roblox.com/asset/?id="
elseif math.abs(e.Floors:findFirstChild(tfloor).Position.y - car.Engine.Position.y) < 3 then
CFrameMove(0.06*d)
else
CFrameMove(0.12*d)
end
wait(0.01)
end
table.remove(queue, FindIn(queue, tfloor))
OpenDoors()
if #queue == 0 or GetNextFloor() == nil then
cardir = 0
elseif GetNextFloor() < cfloor then
cardir = -1
elseif GetNextFloor() > cfloor then
cardir = 1
end
if cardir == 1 or cfloor == 1 then
e.IndicatorLED.Display.Arw.Texture = "http://www.roblox.com/asset/?id="
car.U.Texture.Texture = "http://www.roblox.com/asset/?id=67954829"
car.Logo.ding:Play()
elseif cardir == -1 or cfloor == #e.Floors:GetChildren() then
e.IndicatorLED.Display.Arw.Texture = "http://www.roblox.com/asset/?id="
car.D.Texture.Texture = "http://www.roblox.com/asset/?id=67954829"
car.Logo.ding:Play()
wait(0.3)
car.Logo.ding:Play()
end
enabled = true
for i = 1, 7 do
if enabled == false then return end
wait(1)
end
while GetNextFloor() == nil do
wait(1)
end
tfloor = GetNextFloor()
CloseDoors()
if tfloor > cfloor then
Move(1)
else
Move(-1)
end
end
function OpenDoors()
if closed == false then return end
Closed = false
for x = 1, 80 do
car.Door1.CFrame = car.Door1.CFrame * CFrame.new(0,0,-0.025)
car.Door2.CFrame = car.Door2.CFrame * CFrame.new(0,0,0.025)
e:findFirstChild("Floor"..cfloor).Door1.CFrame = e:findFirstChild("Floor"..cfloor).Door1.CFrame * CFrame.new(0,0,-0.025)
e:findFirstChild("Floor"..cfloor).Door2.CFrame = e:findFirstChild("Floor"..cfloor).Door2.CFrame * CFrame.new(0,0,0.025)
wait(0.01)
end
end
function CloseDoors()
if Closed ~= false then return end
Closed = 0
for x = 1, 80 do
car.Door1.CFrame = car.Door1.CFrame * CFrame.new(0,0,0.025)
car.Door2.CFrame = car.Door2.CFrame * CFrame.new(0,0,-0.025)
e:findFirstChild("Floor"..cfloor).Door1.CFrame = e:findFirstChild("Floor"..cfloor).Door1.CFrame * CFrame.new(0,0,0.025)
e:findFirstChild("Floor"..cfloor).Door2.CFrame = e:findFirstChild("Floor"..cfloor).Door2.CFrame * CFrame.new(0,0,-0.025)
wait(0.01)
end
Closed = true
end
function QueueCall(button)
if button.Name == "BELL" then
if not BellEn then return end
BellEn = false
button.music.Looped = true
button.music:Play()
wait(2)
button.music:Stop()
BellEn = true
return end
if button.Name == "dc" then
button.Button.Texture = "http://www.roblox.com/asset/?id=79508711"
wait(0.1)
button.Button.Texture = "http://www.roblox.com/asset/?id=79508665"
if not enabled then return end
local targ1 = GetNextFloor()
if targ1 ~= nil then
tfloor = targ1
CloseDoors()
if tfloor > cfloor then
Move(1)
else
Move(-1)
end
end
return end
if (string.sub(button.Name,2) == tostring(cfloor)) and (string.sub(button.Name,2) == tostring(tfloor)) then
button.Button.Texture = "http://www.roblox.com/asset/?id=79508711"
wait(0.1)
button.Button.Texture = "http://www.roblox.com/asset/?id=79508665"
return end
button.Button.Texture = "http://www.roblox.com/asset/?id=79508711"
if tfloor == string.sub(button.Name,2) then return end
if FindIn(queue, tonumber(string.sub(button.Name,2)))~=nil then return end
if initial == true then
initial = false
CloseDoors()
tfloor = tonumber(string.sub(button.Name,2))
table.insert(queue,tonumber(string.sub(button.Name,2)))
Move(1)
else
if enabled then
table.insert(queue,tonumber(string.sub(button.Name,2)))
elseif cardir == 1 and tonumber(string.sub(button.Name,2)) < tfloor and tonumber(string.sub(button.Name,2)) > cfloor then
tfloor = tonumber(string.sub(button.Name,2))
table.insert(queue,tfloor)
elseif cardir == -1 and tonumber(string.sub(button.Name,2)) > tfloor and tonumber(string.sub(button.Name,2)) < cfloor then
tfloor = tonumber(string.sub(button.Name,2))
table.insert(queue,tfloor)
else
table.insert(queue,tonumber(string.sub(button.Name,2)))
end
end
end
function PassingChime()
car.Logo.music:Play()
wait(0.3)
car.Logo.music:Stop()
end
e.IndicatorLED.Value.Changed:connect(PassingChime)
for x = 1, #e.CarButtons:GetChildren() do
e.CarButtons:GetChildren()[x].ClickDetector.MouseClick:connect(function() QueueCall(e.CarButtons:GetChildren()[x]) end)
end
for g = 1, #e.HallButtons:GetChildren() do
e.HallButtons:GetChildren()[g].ClickDetector.MouseClick:connect(function() QueueCall(e.HallButtons:GetChildren()[g]) end)
end
