[SOLVED] Chunk generation help

3rd post of this because no one has helped, and the posts are dying…

Alright, so I’m making a backrooms game. It is infinite. No exit… But an infinite backrooms just by building has 2 problems:

  1. It’s literally impossible
  2. Even if it’s possible, it will start a house fire

So I am trying to use chunks to do this, and I was referring to this video, but I can’t figure out how to make everything generate where it should

So, I am just not sure how I can have everything generate in a square extending about 150 studs from the HumanoidRootPart.

I got the idea to reply to keep it alive, so I don’t have to make a bunch of new posts because no one, out of the 41 views on this, has replied… Not 1 reply.

local X, Z = 11, 11
local LastChunk = nil
local Tick = 0

local Segment = {}
Segment.__Index = Segment

function Segment.new(SegmentPosX, SegmentPosZ)
	local SEGMENTS = {
		s1 = script.Segment1,
		s2 = script.Segment2,
		s3 = script.Segment3,
		s4 = script.Segment4,
		s5 = script.Segment5,
		s6 = script.Segment6,
		s7 = script.Segment7,
		s8 = script.Segment8
	}

	game.Players.PlayerAdded:Connect(function(plr)
		plr.CharacterAdded:Connect(function(char)
			while char.Humanoid.Health > 0 do
				task.wait()
				
				if LastChunk == nil or (LastChunk.Position  - char.HumanoidRootPart.Position).Magnitude <= 15 then
					local segmentToUse = math.random(1, 8)
					local segmentCopy
					if segmentToUse == 1 then
						segmentCopy = SEGMENTS.s1:Clone()
					elseif segmentToUse == 2 then
						segmentCopy = SEGMENTS.s2:Clone()
					elseif segmentToUse == 3 then
						segmentCopy = SEGMENTS.s3:Clone()
					elseif segmentToUse == 4 then
						segmentCopy = SEGMENTS.s4:Clone()
					elseif segmentToUse == 5 then
						segmentCopy = SEGMENTS.s5:Clone()
					elseif segmentToUse == 6 then
						segmentCopy = SEGMENTS.s6:Clone()
					elseif segmentToUse == 7 then
						segmentCopy = SEGMENTS.s7:Clone()
					else
						segmentCopy = SEGMENTS.s8:Clone()
					end

					if LastChunk ~= nil then
						local offset = CFrame.new(0 ,0, (segmentCopy.Size.Z/2) + (LastChunk.Size.Z/2) )
						segmentCopy.CFrame = LastChunk.CFrame:ToWorldSpace(offset)

						LastChunk = segmentCopy
					else
						segmentCopy.Position = Vector3.new(char.HumanoidRootPart.Position.X, 0.5, char.HumanoidRootPart.Position.Z)
						LastChunk = segmentCopy
					end

					segmentCopy.Parent = workspace.Backrooms
				end
			end
		end)
	end)
end


return Segment

This is the custom way I think of procedural generation, you can use the check magnitude thing later on to destroy or put away the segments, in this case I used plain parts so if you have models you’d use their primaryparts and define a new floor position which I had mine set as 0.5.

It generates only in Z direction though but it should give a brief idea of how you could go about it.

I had another code that did just this, but when I tried to generate it in the X direction at the same time, the X was offset, which is why I was trying to get everything to generate at once.

I’ll try to combine it will the X direction and see if it actually works this time.

You could use raycast function to check each block on X and if it returns nothing then generate block on the right/left of that block

If you wish to have like lets say 2 bricks on each side then have a variable that says current amount generated for the current segment’s side and if its up to par move onto next brick

So I’m having a problem with them generating, they all spawn inside each other and my CPU exploded.

I took a screenshot:

That’s basically during the game trying to load it, and me trying to exit.

I edited the code to be this:

local Tick = 0

local Segment = {}
Segment.__Index = Segment

function Segment.new(SegmentPosX, SegmentPosZ)
	local SEGMENTS = {
		s1 = script.Segment1,
		s2 = script.Segment2,
		s3 = script.Segment3,
		s4 = script.Segment4,
		s5 = script.Segment5,
		s6 = script.Segment6,
		s7 = script.Segment7,
		s8 = script.Segment8
	}

	game.Players.PlayerAdded:Connect(function(plr)
		plr.CharacterAdded:Connect(function(char)
			while char.Humanoid.Health > 0 do
				task.wait()

				if LastChunk == nil or (LastChunk.PrimaryPart.Position  - char.HumanoidRootPart.Position).Magnitude <= 15 then
					local segmentToUse = math.random(1, 8)
					local segmentCopy
					if segmentToUse == 1 then
						segmentCopy = SEGMENTS.s1:Clone()
					elseif segmentToUse == 2 then
						segmentCopy = SEGMENTS.s2:Clone()
					elseif segmentToUse == 3 then
						segmentCopy = SEGMENTS.s3:Clone()
					elseif segmentToUse == 4 then
						segmentCopy = SEGMENTS.s4:Clone()
					elseif segmentToUse == 5 then
						segmentCopy = SEGMENTS.s5:Clone()
					elseif segmentToUse == 6 then
						segmentCopy = SEGMENTS.s6:Clone()
					elseif segmentToUse == 7 then
						segmentCopy = SEGMENTS.s7:Clone()
					else
						segmentCopy = SEGMENTS.s8:Clone()
					end

					if LastChunk ~= nil then
						local offset = CFrame.new(0 ,0, (segmentCopy.PrimaryPart.Size.Z/2) + (LastChunk.PrimaryPart.Size.Z/2) )
						segmentCopy:SetPrimaryPartCFrame(CFrame.new(Vector3.new(LastChunk.PrimaryPart.CFrame:ToWorldSpace(offset))))

						LastChunk = segmentCopy
					else
						segmentCopy:SetPrimaryPartCFrame(CFrame.new(Vector3.new(char.HumanoidRootPart.Position.X, 0.5, char.HumanoidRootPart.Position.Z)))
						LastChunk = segmentCopy
					end

					segmentCopy.Parent = workspace.Backrooms
				end
			end
		end)
	end)
end


return Segment

The LastChunk variable needs to be there as to determine if the last chunk has been loaded, otherwise the code will just run an infinite loop and possibly crash you

Not sure if I accidentally deleted that or just didn’t highlight it when pasting the script onto here, so Ima check.

Okay yeah it’s there

And it still happens.

Replying to keep it alive because this is really urgent now I’m on a schedule for the game release because another game of mine needs this game

Keeping it alive still, hopefully I don’t have to make a 4th post on this…

People aren’t able to help you because your code relies on the segment objects in your game, making it hard to work with. Maybe you could provide a place file to give people more insight into the project.

Noclipped…rbxl (49.3 KB)
Here is the file, the segments are under the baseplate
Also why would you need the game file anyway? It’s just equivalent to having 30x30 parts generate in chunks just a flat plane.

I don’t recommend trying to run it in it’s current state

1 Like

Sorry, took a while since I was occupied with one of my own projects, but here’s my fixed version of the place file you sent me. I had to redesign the segments to make them work with chunk generation, but luckily they’re just models, so you can alter them if needed. Also improved the way segments are handled, so that you don’t have to manually add them to a table. Just parent them to the script like you did before and it will work. I basically had to heavily expand and overhaul the whole scripting part since most of the functionality needed for chunk generation was either missing or not working as intended. You can take a look at the place file and tell me if that fits your needs.

Noclipped…rbxl (56.3 KB)

It was missing because I just didn’t know what to put in the script there.
No idea at all.

Hey man, I also have made something after taking a look at this thread, check it out and ask me for information if you want.

1 Like

Sorry to be late, but I believe you can generate chunks on chunks lol