Advertisement
HowToRoblox

DungeonGenerator

Sep 25th, 2022 (edited)
2,372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.22 KB | None | 0 0
  1. local rooms = 50 --Change this to however many number of rooms you want the dungeon to have
  2.  
  3. local roomStorage = game.ServerStorage:WaitForChild("DungeonsServerStorage"):WaitForChild("ROOMS")
  4. local doorStorage = game.ServerStorage.DungeonsServerStorage:WaitForChild("DOOR TYPES")
  5.  
  6.  
  7. local roomsContainer = Instance.new("Folder")
  8. roomsContainer.Name = "Dungeon"
  9. roomsContainer.Parent = workspace
  10.  
  11.  
  12. function createRoom(lastRoom)
  13.    
  14.     local currentRooms = #roomsContainer:GetChildren()
  15.    
  16.     if currentRooms < rooms then
  17.         local numRooms = math.random(1, math.clamp(rooms - currentRooms, 1, 4))
  18.        
  19.         local walls = lastRoom.Walls:GetChildren()
  20.  
  21.         for i = #walls, 2, -1 do
  22.             local j = Random.new():NextInteger(1, i)
  23.             walls[i], walls[j] = walls[j], walls[i]
  24.         end
  25.        
  26.         local createdRooms = {}
  27.        
  28.         for i = 1, numRooms do
  29.            
  30.             if walls[i] and walls[i]:IsA("Part")  then
  31.                
  32.                 local wallCF = walls[i].CFrame
  33.                
  34.                 local doorsList = doorStorage:GetChildren()
  35.                 local newWall = doorsList[math.random(1, #doorsList)]:Clone()
  36.                 newWall.CFrame = wallCF
  37.                 newWall.Parent = walls[i].Parent
  38.                
  39.                 local wallName = walls[i].Name
  40.                 walls[i]:Destroy()
  41.                
  42.                
  43.                 local roomCFrame = lastRoom.Floor.CFrame + (wallCF.LookVector * lastRoom.Floor.Size.X)
  44.                 local isRoom = false
  45.                
  46.                 for x, room in pairs(roomsContainer:GetChildren()) do
  47.                    
  48.                     if room.Floor.Position == roomCFrame.Position then
  49.                         isRoom = true
  50.                        
  51.                         if wallName == "Back Wall" and room.Walls:FindFirstChild("Front Wall") then
  52.                             room.Walls["Front Wall"]:Destroy()
  53.                         elseif wallName == "Front Wall" and room.Walls:FindFirstChild("Back Wall") then
  54.                             room.Walls["Back Wall"]:Destroy()
  55.                         elseif wallName == "Left Wall" and room.Walls:FindFirstChild("Right Wall") then
  56.                             room.Walls["Right Wall"]:Destroy()
  57.                         elseif wallName == "Right Wall" and room.Walls:FindFirstChild("Left Wall") then
  58.                             room.Walls["Left Wall"]:Destroy()
  59.                         end
  60.                     end
  61.                 end
  62.                
  63.                 if not isRoom then
  64.                    
  65.                     local roomsList = roomStorage:GetChildren()
  66.                     local newRoom = roomsList[math.random(1, #roomsList)]:Clone()
  67.                     newRoom.PrimaryPart = newRoom.Floor
  68.                    
  69.                     newRoom:SetPrimaryPartCFrame(roomCFrame)
  70.                    
  71.                     if wallName == "Back Wall" and newRoom.Walls:FindFirstChild("Front Wall") then
  72.                         newRoom.Walls["Front Wall"]:Destroy()
  73.                     elseif wallName == "Front Wall" and newRoom.Walls:FindFirstChild("Back Wall") then
  74.                         newRoom.Walls["Back Wall"]:Destroy()
  75.                     elseif wallName == "Left Wall" and newRoom.Walls:FindFirstChild("Right Wall") then
  76.                         newRoom.Walls["Right Wall"]:Destroy()
  77.                     elseif wallName == "Right Wall" and newRoom.Walls:FindFirstChild("Left Wall") then
  78.                         newRoom.Walls["Left Wall"]:Destroy()
  79.                     end
  80.                    
  81.                     newRoom.Parent = roomsContainer
  82.                    
  83.                     table.insert(createdRooms, newRoom)
  84.                 end
  85.             end
  86.         end
  87.        
  88.         for i, createdRoom in pairs(createdRooms) do
  89.             createRoom(createdRoom)
  90.         end
  91.     end
  92. end
  93.  
  94.  
  95. local startRoom = roomStorage:WaitForChild("Empty Room"):Clone()
  96. startRoom.PrimaryPart = startRoom.Floor
  97. startRoom:SetPrimaryPartCFrame(CFrame.new(Vector3.new(0, startRoom.Floor.Size.Y/2, 0)))
  98.  
  99. startRoom.Parent = roomsContainer
  100.  
  101. createRoom(startRoom)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement