Bug with procedural generation

Hello, I need to make procedural generation in my game but it doesnt work normally

For some reason, the rooms spawn under each other

I tried use other metods like: SetPrimaryPartCFrame, Moveto and other but it doesnt work.

local rs = game:GetService("ReplicatedStorage")
local GameRooms = workspace.GameValues.GameRooms
local Rooms = workspace.Rooms
local NewRoom
local OldRoom = workspace.Rooms.Corridor1

local function spawn_room()
	print("Trying")
	local rooms = rs.Rooms:GetChildren()
	local randomRoom = rooms[math.random(1, #rooms)]
	print("RandomRoom")
	NewRoom = rs.Rooms.Corridor1:Clone() -- randomRoom:Clone()
	NewRoom.Parent = Rooms
	print("Clone")
	NewRoom:PivotTo(OldRoom.SpawnPart.CFrame)
	print("Pivot")
	GameRooms.Value += 1
	OldRoom = NewRoom
end

while GameRooms.Value <= 1000 do
	task.wait(1)
	spawn_room()
end

MyPlace.rbxl (61.0 KB)

Is the pivot point of the new room set correctly? If the point was its default (center of model), then the center of the new room will be moved directly onto the previous spawn part. If the spawn location has a lower position than the previous room pivot, the rooms will keep spawning in a downwards fashion.

1 Like

The pivot point of the old model is installed correctly, if you manually install a new room, everything works correctly

but the script does this:

You’re not doing any calculations to change NewRoom and then reset OldRoom to that position. There should be something calculating the NewRoom’s new Position and Orientation and placing it, then resetting the OldRoom to NewRoom, and doing the same calculation above for the next placement of the cloned NewRoom.

Don’t use simple prints like print("Clone") which only tell you if something worked or not. Usually that’s not enough information.
Try
print("Cloned, OldRoom.SpawnPart.CFrame = ", OldRooom.SpawnPart.CFrame)
to let you know the information that’s being used for the issue you’re having.

Also, don’t Parent an item until you’ve got all it’s parameters and Propeties set.

2 Likes

I tryied to do calculating orientation, but script just doesnt work

Your script just keeps placing them at the starting Position since there’s no calculation.
Place the 2 hallways together like in your first image.
Figure out the Position of the first hallway SpawnPart. Lets say it starts a 100, 0, 100 to make it easy.
Figure out the Position of the second hallway SpawnPartwhen it’s placed properly. Let’s say the new Position is 140, 0, 140.
Subtract the first Position from the second which in this example would be 40, 0, 40 which means every new room would be placed in increments of those values.
Add that value (NewRoom) to the current Position (OldRoom) every time you place the next one. In this example the next room would be placed at 180, 0, 180. The next would be 220, 0, 220 and so on.

1 Like

I rewrote the script, but for some reason it gives an error

local rs = game:GetService("ReplicatedStorage")
local GameRooms = workspace.GameValues.GameRooms
local Rooms = workspace.Rooms
local NewRoom
local OldRoom = workspace.Rooms.Corridor1
local position

local function spawn_room()
	local rooms = rs.Rooms:GetChildren()
	local randomRoom = rooms[math.random(1, #rooms)]
	NewRoom = randomRoom:Clone()
	position = OldRoom.SpawnPart.CFrame - NewRoom.SpawnPart.CFrame
	print("Cloned, NewRoom:PivotTo",position)
	NewRoom:PivotTo(position)
	GameRooms.Value += 1
	OldRoom = NewRoom
	OldRoom.Name = GameRooms.Value
	NewRoom.Parent = Rooms
	NewRoom = nil
end

while GameRooms.Value <= 1000 do
	task.wait(1)
	spawn_room()
end

Use PivotTo() if that doesn’t work set the PrimaryPart of the Room to the door you want to attach, OR set the door orientation the same.

1 Like

If you read the error it tells you what’s wrong.
A Position is a Vector3 value made up of 3 numbers separated by commas (your X, Y, Z).
A CFrame is an array of values that tells you the Position and Orientation of the item.

Either use Position for all the items in your calculation, CFrames for all the items, or convert your CFrame to Vector3 Position.

i found bug, it was spawnpart of Rooms! Thanks