Editing a Generation Script to Include 1 Time Generation Rooms

I have a script that generates a large area of premade rooms. It works great and all that, but now I’m trying to edit in the ability for it to generate specific rooms only once.

I thought about maybe using a BoolValue and having the script check if it’s true or false. If it was false, it could generate the room, but if it was true, it wouldn’t generate the room. From past experience, I know there is almost a better way of doing things, so any help is appreciated!

Script:

local plots = 10
for a = -plots, plots do
	for b = -plots, plots do
		local size = 51
		local clone = game.ServerStorage.Rooms["Room"..math.random(1,26)]:Clone()
		clone.Parent = workspace
		clone:SetPrimaryPartCFrame(CFrame.new(a*size, 1, b*size))
	end
end

(A long while back I made a topic for creating this generation script and a very kind DevForum user helped me out with it, so all credit goes to them for the script.)

You can have a table with a “budget” for certain types of room. You can generate this table from your ServerStorage.Rooms like so:

local possibleRooms = {}

for _, room in game.ServerStorage.Rooms:GetChildren() do
  table.insert(possibleRooms, {RoomModel = room, Budget = 1}
end

This will create a list of tables which each contain RoomModel pointing to the instance that needs cloned, and a Budget number that tells how many can be generated. When you generate a room, reduce this number. If it reaches zero, remove it from the list so it can’t be selected.

--If any room starts with a Budget of 0, it will still generate and Budget will count down into negative numbers
--You can invert this to count how many rooms of each type were generated.
local function SelectRoom()
  local totalChoices = #(possibleRooms)
  local choice = math.random(1, totalChoices)
  local clone = possibleRooms[choice].RoomModel:Clone()
  --Generate your room

  --Check budget and remove if zero
  possibleRooms[choice].Budget -= 1

  if possibleRooms[choice].Budget == 0 then
    table.remove(possibleRooms, choice)
  end
end

You can load the Budget value from an Attribute or IntValue inside the RoomModel. You can choose any method you like for allowing a room to have infinite budget, such as using Budget = -1 to mean unlimited.
This also gives you a nice way of counting how many of each room was actually generated, since you can then compare the remaining budget values to the original values.

2 Likes

I’m gonna do some experimenting with this and get back to you on it. I definitely love the idea of a budget

So I finally got around to working on this. I merged the two scripts, but I don’t think I did it correctly… I also added an IntValue into each room model named “Budget”. I’m not sure if this has something to do with it, but each room is named something different. Anyway, when testing, nothing actually appears and the output is empty. I’m sure I’ve missed something entirely, but here are the scripts and a section of the explorer hierarchy:

local possibleRooms = {}

for _, room in game.ServerStorage.Rooms:GetChildren() do
	table.insert(possibleRooms, {RoomModel = room, Budget = 1})
end

local rooms = 10

local function SelectRoom()
	local totalChoices = #(possibleRooms)
	local choice = math.random(1, totalChoices)
	local clone = possibleRooms[choice].RoomModel:Clone()
for a = -rooms, rooms do
	for b = -rooms, rooms do
		local size = 51
		local roomsFolderContents = game.ServerStorage.Rooms:GetChildren()
		local clone = roomsFolderContents[math.random(1, #roomsFolderContents)]:Clone()
		clone.Parent = workspace
		clone:SetPrimaryPartCFrame(CFrame.new(a*size, 1, b*size))
			possibleRooms[choice].Budget -= 1

			if possibleRooms[choice].Budget == 0 then
				table.remove(possibleRooms, choice)
			end
		end
	end
end

image

EDIT: I was able to get it working with a few small edits! Thanks!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.