How can I generate a room once between two values?

Hello devs,

Right now I am working on a “Rooms & Doors” inspired game. I’m trying to make a specific room (“PowerRoom”, as shown in the script below) generate between A-0001 and A-0020 only once, just like in Rooms and Doors.

local ServerStorage = game:GetService("ServerStorage")

local Events = ServerStorage.Events
local Values = ServerStorage.Values
local PreludeRooms = ServerStorage.PreludeRooms:GetChildren()
local PreludeRoomsAmount = 20


local RandomGeneration = Random.new()

Events.DoorOpen.Event:Connect(function()
	local RoomClone = PreludeRooms[RandomGeneration:NextInteger(1, #PreludeRooms)]:Clone()
	Values.RoomNumber.Value += 1
	RoomClone:PivotTo(Values.CurrentRoom.Value.Exit.CFrame)
	RoomClone.Parent = game.Workspace
	RoomClone:FindFirstChild("Door").Main.Sound.PlaybackSpeed = RandomGeneration:NextNumber(0.5,1)
	Values.CurrentRoom.Value = RoomClone
	
	--Generate power room between the first and the last of the prelude rooms(A-0001 to A-0020)
	if Values.RoomNumber.Value == math.random(1, PreludeRoomsAmount) then
		local PowerRoom = PreludeRooms.PowerRoom:Clone()
		Values.RoomNumber.Value += 1
		PowerRoom:PivotTo(Values.CurrentRoom.Value.Exit.CFrame)
		PowerRoom.Parent = game.Workspace
		PowerRoom:FindFirstChild("Door").Main.Sound.PlaybackSpeed = RandomGeneration:NextNumber(0.5,1)
		Values.CurrentRoom.Value = PowerRoom
	end
	
	--Turn lights off at game start
	if Values.PreludeLights.Value == false then
		local lights = RoomClone:GetChildren()
		for _, light in pairs(lights) do
			if light:IsA("Model") and light.Name == "light" then
				light["Cube.001"]:FindFirstChild("Attachment"):FindFirstChild("PointLight").Brightness = 0.1
				light["Cube.001"].Color = Color3.fromRGB(106,106,106)
			end
		end
	else
		-- Turn lights on when the switch is turned
		local lights = RoomClone:GetChildren()
		for _, light in pairs(lights) do
			if light:IsA("Model") and light.Name == "light" then
				light["Cube.001"]:FindFirstChild("Attachment"):FindFirstChild("PointLight").Brightness = 1
				light["Cube.001"].Color = Color3.fromRGB(255, 255, 255)
			end
		end
	end
	
	--Room sign text formatting
	if Values.RoomNumber.Value < 10 then
		RoomClone:FindFirstChild("RoomSign").Text.SurfaceGui.RoomNumber.Text = "000"..Values.RoomNumber.Value 
	elseif Values.RoomNumber.Value < 100 then
		RoomClone:FindFirstChild("RoomSign").Text.SurfaceGui.RoomNumber.Text = "00"..Values.RoomNumber.Value 
	else
		RoomClone:FindFirstChild("RoomSign").Text.SurfaceGui.RoomNumber.Text = "0"..Values.RoomNumber.Value 
	end
end)

As you can see, I’ve tried math.random(min, max) here, hoping it would work, as it generates values between the minimum and maximum values given in the brackets.

1 Like

Please take note that this script is being used in ServerScriptService.

1 Like

I don’t think I quite understand whay you’re really asking. Do you mean choose between 1 and 20 and not get the same number twice?
Like if you put the numbers that have been used in a list, then loop the math.random until it’s a number that hasnt been used?
Also I’m guessting the events you’re using are BindableEvents right?

2 Likes

Yes, this is what I meant.

Also you are correct on me using BindableEvents.

1 Like

I would just make something like:

local IsCreated

--[[when the room is created, make IsCreated true

make the check, if IsCreated == true then redo the function to give another number]]