How can I generate a random dungeon after another one of them is completed?

Heya, I’m making a Rogue-Like dungeon horror game, and I have the basics, I just don’t know how to adapt them to the game I’m specifically trying to make.
In the game, you go in an elevator and delve deep into a facility for as long as you can, each floor is randomly generated and you need to press buttons to advance to the next floor
I have the random generation script (kind of) but I don’t know how I’d go about unloading the map and generating a random new one each time the floor is completed.
Here’s the generation code:

--//Services
local pfs = game:GetService("PathfindingService")

--//Setup
local f = Instance.new("Folder", workspace)
f.Name = "CurrentGeneratedRooms"

for _,v in pairs(workspace.MapGrid:GetChildren()) do
	v.Transparency = 1
end
for _,v in pairs(game.ReplicatedStorage.Rooms:GetChildren()) do
	v.PrimaryPart = v.Floor
end

local rooms = game.ReplicatedStorage.Rooms:GetChildren()
local rotations = {0,90,180,270} -- directions the rooms can be rotated in
local roomGenerated = false

--//Functions
local function generateRooms()
	f:ClearAllChildren()
	
	for _,floor in pairs(workspace.MapGrid:GetChildren()) do
		local chosenRoom = rooms[math.random(1, #rooms)]:Clone() --Pick random Room
		chosenRoom:SetPrimaryPartCFrame(floor.CFrame * CFrame.Angles(0, math.rad(rotations[math.random(1,#rotations)]), 0))
		chosenRoom.Parent = f
	end
end
local function GiveLoadingScreen(player)
	local gui = script.Loading:Clone()
	gui.Parent = player.PlayerGui
end

--//Main
--Give Loading Screen
for _,player in pairs(game.Players:GetPlayers()) do 
	GiveLoadingScreen(player)
end
game.Players.PlayerAdded:Connect(function(player)
	if roomGenerated == false then
		GiveLoadingScreen(player)
	end
end)

--Generate Rooms
local path = nil
repeat
	generateRooms() --Generate rooms randomly
	
	--Check if there is a path from start to end
	path = pfs:CreatePath()
	path:ComputeAsync(workspace.Start.Floor.Position, workspace.End.Floor.Position)
until
	path.Status == Enum.PathStatus.Success

roomGenerated = true

for _,player in pairs(game.Players:GetPlayers()) do 
	if player.PlayerGui:FindFirstChild("Loading") then
		player.PlayerGui.Loading:Destroy()
	end
end

I have the rooms that can be generated stored in ReplicatedStorage, the generation script is in ServerScriptService and the generation template is located in the workspace.
I’d love any help on this, I’m really looking forward to developing this
Thank you in advance :slight_smile:

1 Like

I just skimmed through your code, and I do not think there is anything that checks when a dungeon is complete. I would make a “complete condition” that turns to true when all the buttons have been pushed. An easy way to do this would be to put a bool value in each button, set it to true when it has been pushed, and whenever a button gets pressed check if all the other buttons are true as well. Then just delete the old dungeon, use your GiveLoadingScreen Function on the player, and load a new one.

1 Like

this does give me a general idea, however as mentioned i am still very new to scripting, so what API references can i use to achieve this? or what stuff would i have to add to my code to make this?

I see you have used for loops, I would run one of these that checks through all the buttons whenever a button gets pushed. I would put this in a function like so:
(Typing this from phone, sorry if there are any errors but it should give you the general idea)

local function CheckButtons()
—side note, for this example I would put all the buttons in a folder when you generate the dungeon.

for _, Button in ipairs(workspace.ButtonFolder:GetChildren()) do
if Button.HasBeenClicked.Value == false then
—In this example has been clicked is a book value in the button.
return false
end
end
return true
end

next, I would do this whenever a button is clicked:

Button.HasBeenClicked = true
if CheckButtons() then
—generate new dungeon here!
end

Hope this helps! If you have any questions, feel free to reply.

1 Like