Yeah, I’m working on an elevator. Don’t ask me why.
Alright, I’m having some technical difficulties on script this elevator. Every time I ride on it, it thought that the elevator is arrived.
You can hear multiple “Dings” idk why. Come on, help me! I have been stuck on this for a 3 days. Help me to find the serious problem.
Video:
robloxapp-20200404-1651243.wmv (2.6 MB)
Script:
local ObjMoving = script.Parent.Moving
local Moving = script.Parent.Moving.Value
local Direction = true ---- true means up, false mean down ----
--Actions--
local function ChangeDirection(Dir)
Direction = Dir
end
local function ArrivalAnimation()
script.Parent.Moving.Value = true
Module:Open()
wait(5)
Module:Close()
script.Parent.Moving.Value = false
end
local function Move()
Module:Move()
ArrivalAnimation()
end
local function RequestRemover()
local Folder = script.Parent.Requests:GetDescendants()
for r = 1,#Folder do
if Folder[r].Name == tostring(script.Parent.CurrentFloor.Value) then
Folder[r]:Destroy()
end
end
end
local function CalculationCompleted()
RequestRemover()
Move()
end
local function Calculate() ---- Calculation Start ----
if Moving == false then
local Table = {} ---- A table that contain all the requested floor, it should not have a repeat values
local DistanceTable = {} ---- A table that stores and the distances, negative values included / No repeat values / The Nth place can be match to Table(the one above this line)
local RequestFloors = script.Parent.Requests:GetDescendants()
---- Finding all the floors that requested in the elevator or outside ----
for i =1,#RequestFloors do
if RequestFloors[i].ClassName ~= "Folder" and table.find(Table,tonumber(RequestFloors[i].Name)) == nil then
table.insert(Table,#Table+1,tonumber(RequestFloors[i].Name))
end
end
---- List all the distances to destination ----
for a = 1,#Table do
table.insert(DistanceTable,a,Table[a] - script.Parent.CurrentFloor.Value) ---- If the value is positive, it means that the request floor is above the elevator
end
---- Find out if there is a requested floor above,under or at the same floor to the current floor ----
local Above = 0 ---- Indicates how many floors above the elevator are requested
local Under = 0 ---- Indicates how many floors Under the elevator are requested
local Same = 0 ---- Indicates if the current elevator floor is requested. 0 = No/ 1 = Yes
for b = 1,#DistanceTable do
if DistanceTable[b] > 0 then
Above = Above + 1
end
if DistanceTable[b] < 0 then
Under = Under + 1
end
if DistanceTable[b] == 0 then
Same = Same + 1
end
end
---- Find the nearest requested floor if it wants to go down(Finding the biggest number that is negative)(Find from DistanceTable) ----
local Biggest = -100 ---- The biggest number that is negative so far. After the loop complete, it will be the result.
local N = 0 ---- The place of the biggest number, can be used to find which floor is the nearest instead of distance.
if Under > 0 then ---- Identify if there is a requested floor under the current floor
for c = 1,#DistanceTable do
if DistanceTable[c] > Biggest then
if DistanceTable[c] < 0 then
Biggest = DistanceTable[c]
N = c
end
end
end
end
---- Find the nearest requested floor if it wants to go up ----
local Smallest = 100
local n = 0
if Above > 0 then
for c = 1,#DistanceTable do
if DistanceTable[c] < Smallest then
if DistanceTable[c] > 0 then
Smallest = DistanceTable[c]
n = c
end
end
end
end
---- Change Direction if the elevator can't go up/down anymore ----
if Above == 0 or Under == 0 then
if Above == 0 and Under > 0 then
ChangeDirection(false)
end
if Above > 0 and Under == 0 then
ChangeDirection(true)
end
end
---- Decide where to go ----
if Direction == true then
print(tostring(Table[n]).."n")
script.Parent.FloorToReach.Value = Table[n] ---- Change the value in order to make the Module knows where to move
else
print(tostring(Table[N]).."N")
script.Parent.FloorToReach.Value = Table[N]---- Change the value in order to make the Module knows where to move
end
---- If the floor is the same ----
if Same == 1 then
script.Parent.FloorToReach.Value = script.Parent.CurrentFloor.Value
end
---- Tells the script that the calulation has completed if there is a request ----
CalculationCompleted()
end
end ---- Calculation End ----
--Firing Events--
--Fire once a request is added--
script.Parent.Requests.DescendantAdded:Connect(function()
local RequestInfos = script.Parent.Requests:GetDescendants()
if Moving == false and #RequestInfos > 2 then ---- Make sure that it won't fire when moving ----
Calculate()
end
end)
--Fire once a request is removed--
script.Parent.Requests.DescendantRemoving:Connect(function(Item)
local RequestInfos = script.Parent.Requests:GetDescendants()
local A = 0
if #RequestInfos > 2 then
for i = 1,#RequestInfos do ---- make sure that there is no requests, if there is a request then delete it. ----
A = A + 1
if RequestInfos[i].Name == Item.Name then
wait()
RequestInfos[i]:Destroy()
end
end
if A == 0 and Moving == false then ---- indentify that there is no repeat request & not moving, then start a calculation. ----
Calculate()
end
end
end)
--Fire once the status changes--
ObjMoving.Changed:Connect(function()
local requests = script.Parent.Requests:GetDescendants()
if Moving == false and #requests > 2 then ---- Make sure that it won't fire when moving or no requests----
Calculate()
end
end)