I am trying to make this procedural generation code run as fast as possible. Please let me know what I can change.
local replicatedStorage = game:GetService("ReplicatedStorage")
local rooms = replicatedStorage.Rooms:GetChildren()
local placedRooms = workspace.PlacedRooms
local doorFolder = workspace.Doors
function setParentToDoors(children)
for i,v in children do
v.Parent = doorFolder
end
end
function checkIfSame(pos)
local samePos = false
for i,v in placedRooms:GetChildren() do
if (pos - v.Hitbox.Position).magnitude < 0.001 then
samePos = true
else
samePos = false
end
end
return samePos
end
local firstRoom = rooms[math.random(1, #rooms)]:Clone()
firstRoom.Parent = workspace.PlacedRooms
firstRoom.PrimaryPart = firstRoom.Doors:GetChildren()[1]
firstRoom:SetPrimaryPartCFrame(CFrame.new(0,50,0))
firstRoom.PrimaryPart = nil
setParentToDoors(firstRoom.Doors:GetChildren())
function generateMaze(iterations)
for i = 1, iterations do
local doors = doorFolder:GetChildren()
if #doors > 0 then
for i,x in doors do
local roomList = {}
for i = 1, #rooms do
table.insert(roomList,i)
end
for i = 1, #rooms do
local random = math.random(1, #roomList)
local room = rooms[roomList[random]]:Clone()
table.remove(roomList,random)
room.PrimaryPart = room.Doors:GetChildren()[math.random(1, #room.Doors:GetChildren())]
room:SetPrimaryPartCFrame(CFrame.new(x.Position))
room.Parent = workspace
local rotate = false
local turn = 0
local notTouching = false
for i = 1,5 do
rotate = false
local inPart = workspace:GetPartsInPart(room.Hitbox)
for i,y in inPart do
if y.Name == "Hitbox" then
rotate = true
end
end
if rotate then
turn = turn + 90
room:SetPrimaryPartCFrame(CFrame.new(x.Position)*CFrame.Angles(0,math.rad(turn),0))
else
notTouching = true
end
end
if notTouching then
room.PrimaryPart:Destroy()
setParentToDoors(room.Doors:GetChildren())
room.Parent = workspace.PlacedRooms
else
room:Destroy()
end
x:Destroy()
end
end
else
return
end
end
end
generateMaze(200)