```
local function ManHattan(RoomOne, RoomTwo)
--first
local function Inbetween(Val, ValTwo)
return (Val + (math.abs(Val-ValTwo)/2))
end
return math.sqrt(math.abs( Inbetween(RoomOne["Letters"][1],RoomOne["Letters"][#RoomOne["Letters"]]) - Inbetween(RoomTwo["Letters"][1],RoomTwo["Letters"][#RoomTwo["Letters"]])^2 + math.abs(RoomOne["Number"] - RoomTwo["Number"])^2))
end
local function PathFind(StartingRoom, Destination)
--Initialize both open and closed list
local OpenList={}
local ClosedList={}
--Add the start Room
local CurrentRoom = StartingRoom
local LevelsOfContrivance = 0
table.insert(OpenList, StartingRoom)
--Loop until end is found
while #OpenList > 0 and LevelsOfContrivance < 100 do
wait()
LevelsOfContrivance+=1
--Get the current node
CurrentRoom = OpenList[1]
for i = 1,#OpenList do
if OpenList[i].f < CurrentRoom.f then
CurrentRoom = OpenList[i]
end
end
table.remove(OpenList, table.find(OpenList,CurrentRoom))
table.insert(ClosedList, CurrentRoom)
--Found the goal
--print("CurrentRoom : ".. CurrentRoom["Name"])
if CurrentRoom == Destination then
print("Done")
local path = {}
local current = CurrentRoom
local MoreContrivances = 0
while current.Parent and LevelsOfContrivance < 100 do
MoreContrivances+=1
table.insert(path, current)
current = current.Parent
end
if LevelsOfContrivance >= 100 then
else
return path
end
end
--Generate children
local Children = {}
for j = 1,#OrganisedRooms do
if CurrentRoom["Letters"][#CurrentRoom["Letters"]] + 1 == OrganisedRooms[j]["Letters"][1] and OrganisedRooms[j]["Number"] == CurrentRoom["Number"] then --CheckAdjacentRight
local AdjacentRight = OrganisedRooms[j]
if AdjacentRight then
table.insert(Children,AdjacentRight)
end
end
end
for j = 1,#OrganisedRooms do
if CurrentRoom["Letters"][1] - 1 == OrganisedRooms[j]["Letters"][#OrganisedRooms[j]["Letters"]] and OrganisedRooms[j]["Number"] == CurrentRoom["Number"] then --CheckAdjacentLeft
local AdjacentLeft = OrganisedRooms[j]
if AdjacentLeft then
table.insert(Children,AdjacentLeft)
end
end
end
if CurrentRoom["Name"] == "Elevator" then
for j = 1,#OrganisedRooms do
if (CurrentRoom["Number"] - 1 == OrganisedRooms[j]["Number"]) and OrganisedRooms[j]["Name"] == "Elevator" and OrganisedRooms[j]["Letters"][1] == CurrentRoom["Letters"][1] then
table.insert(Children, OrganisedRooms[j])
end
if (CurrentRoom["Number"] + 1 == OrganisedRooms[j]["Number"]) and OrganisedRooms[j]["Name"] == "Elevator" and OrganisedRooms[j]["Letters"][1] == CurrentRoom["Letters"][1] then
table.insert(Children, OrganisedRooms[j])
end
end
end
for i = 1, #Children do
if table.find(ClosedList,Children[i]) then
else
Children[i].g = CurrentRoom.g + ManHattan(Children[i],CurrentRoom)
Children[i].h = ManHattan(CurrentRoom,Destination)
Children[i].f = Children[i].g + Children[i].h
local BreakWholeLoop = false
for j = 1,#OpenList do
if Children[i] == OpenList[j] and Children[i].g >= OpenList[j].g then
BreakWholeLoop = true
break
elseif Children[i] == OpenList[j] and Children[i].g < OpenList[j].g then
OpenList[j].g = Children[i].g
BreakWholeLoop = true
break
else
end
end
if BreakWholeLoop == true then
break
end
table.insert(OpenList, Children[i])
Children[i]["Parent"] = CurrentRoom
end
end
end
warn("Problem with finding path: Loop at risk of running forever")
end
```

Its a 2D version of the A* Pathfinding Algorithmn with the fact you can only move up/down if in elevator and elevator is above/below it.

Code works perfectly but just sometimes doesnt work because the “OpenList” becomes empty even when there is a path. but this only happens in studio

**(Sorry ill try and explain the code cause it wasnt ment to be shown so ive just used varaible names that i understand.**

**[Letters"] which is still a number value but in the Grid(A-P) its the number equivilent. so if the room size is 2 and at C,D then Letters = 3,4**

**[“Numbers”] is the Y axis equivalent but rooms are never longer than 1 long**

**[“Name”] Obvious**

**[“Object”] the Model in the workspace of the room**

**[“F”],[“G”],[“H”] = Distance current room from adjacent room, Distance from adjacent room and Destination, F = G+H**

**[“Parent”] = The Room next to it that the path came from so it can be traced back to return the path**

** )**