Random Room Generation Help

Hello. I’m having trouble with a random room gen script. (I suck at scripting btw and I haven’t scripted in a while)

Ik this script is messy, but idk any other way to do this.

local rooms = 14
local roomsdone = 0
local totalrooms = 14
local parts = script.Parent
local start = game.Workspace.RoomsStart

repeat
	local currentroom = math.random(1,rooms)
	
	if currentroom == 1 then
		local room1 = script.Parent.Crates1:Clone()
		if roomsdone == 0 then
			room1.Position = start.Position
		end
	end

	if currentroom == 2 then
		local room1 = script.Parent.Crates2:Clone()
		if roomsdone == 0 then
			room1.Position = start.Position
		end
	end

	if currentroom == 3 then
		local room1 = script.Parent.Crates3:Clone()
		if roomsdone == 0 then
			room1.Position = start.Position
		end
	end

	if currentroom == 4 then
		local room1 = script.Parent.Crates4:Clone()
		if roomsdone == 0 then
			room1.Position = start.Position
		end
	end

	if currentroom == 5 then
		local room1 = script.Parent.Crates5:Clone()
		if roomsdone == 0 then
			room1.Position = start.Position
		end
	end

	if currentroom == 6 then
		local room1 = script.Parent.Crates6:Clone()
		if roomsdone == 0 then
			room1.Position = start.Position
		end
	end
	
	if currentroom == 7 then
		local room1 = script.Parent.Crates7:Clone()
		if roomsdone == 0 then
			room1.Position = start.Position
		end
	end
	
	if currentroom == 8 then
		local room1 = script.Parent.Crates8:Clone()
		if roomsdone == 0 then
			room1.Position = start.Position
		end
	end
	
	if currentroom == 9 then
		local room1 = script.Parent.Crates9:Clone()
		if roomsdone == 0 then
			room1.Position = start.Position
		end
	end
	
	if currentroom == 10 then
		local room1 = script.Parent.Crates10:Clone()
		if roomsdone == 0 then
			room1.Position = start.Position
		end
	end
	
	if currentroom == 11 then
		local room1 = script.Parent.Crates11:Clone()
		if roomsdone == 0 then
			room1.Position = start.Position
		end
	end
	
	if currentroom == 12 then
		local room1 = script.Parent.Crates12:Clone()
		if roomsdone == 0 then
			room1.Position = start.Position
		end
	end
	
	if currentroom == 13 then
		local room1 = script.Parent.Crates13:Clone()
		if roomsdone == 0 then
			room1.Position = start.Position
		end
	end
	
	if currentroom == 14 then
		local room1 = script.Parent.Crates14:Clone()
		if roomsdone == 0 then
			room1.Position = start.Position
		end
	end
	
	roomsdone = roomsdone + 1
until roomsdone == 1

I want a certain amount of randomly chosen rooms (out of the 14 currently) to generate in front of each other, the first room going to the CFrame of this specific part, but I can’t seem to even get that to work.

If anyone can provide me with any information or write me a script, it would be much appreciated.

1 Like

I’m guessing the Rooms you referring to are Models.

You cant move the position of a Model or a part with Welds, you must use either PVInstances :PivotTo() or Models :MoveTo()

If my knowledge is correct, these Methods move the Instance/Model based on its PrimaryPart/RootPart, so set the PrimaryPart to the Entrance of the room, so it pivots the entrance of the second room to the Vector3 coordinates (of which you specify) to the exit of the first room.

PivotTo
MoveTo

I am not entirely sure what your problem is, but try using “#rooms” to get the number of rooms.
Not sure if that is the solution, but let me know if it works. :slight_smile:

1 Like