What do you want to achieve? I am making a Backrooms game and I want the Backrooms map to be generated randomly using chunks.
What is the issue? I have absolutely no idea where to go on from here. I want to somehow detect how close a player is to a chunk, according to that, multiple other chunks will be placed around that chunk (Don’t know if this is efficient or not). If there are no chunks nearby, a chunk will be generated at the position of the player and many other chunks surrounding that chunk.
Here’s what I have currently made:
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Assets = ServerStorage:WaitForChild("Assets")
local Level0 = Assets["Level 0"]
local Chunk = Level0.Chunk
local Ceiling = Level0.Ceiling
local CeilingLight = Level0["Ceiling Light"]
local Wall = Level0.Wall
local ExistingChunks = {}
local ChunkSize = 200
local NearbyChunkDistance = ChunkSize * 2
function GenerateChunk(Position)
local Chunk = Chunk:Clone()
Chunk.Size = Vector3.new(ChunkSize, 2, ChunkSize)
Chunk.Name = "Chunk"
Chunk.Anchored = true
Chunk.Parent = workspace
Chunk.Position = Position
table.insert(ExistingChunks, Chunk)
local Front = (Chunk.CFrame * CFrame.new(Chunk.Size.X/2, 0, 0)).Position.X
local Back = (Chunk.CFrame * CFrame.new(Chunk.Size.X/-2, 0, 0)).Position.X
local LeftSide = (Chunk.CFrame * CFrame.new(0,0,Chunk.Size.Z/2)).Position
local RightSide = (Chunk.CFrame * CFrame.new(0,0,Chunk.Size.Z/-2)).Position
local RandomX = math.random(Back, Front)
local RandomZ = math.random(RightSide, LeftSide)
local OriginVector = Vector3.new(RandomX, Chunk.Position.Y + 100, RandomZ)
local Direction = OriginVector
Direction.Y = -Direction.Y
local RaycastResult = workspace:Raycast(OriginVector, Direction)
if RaycastResult then
if RaycastResult.Instance == Chunk then
--[[
Here's the part where the walls and some objects will be
generated into the chunk.
]]
end
end
end
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
-- SOMEHOW SPAWN IN NEW CHUNKS WITH CORRECT POSITIONS
end)
end)
Oh yeah, and this is a server script. I want all the new chunks generated to be visible to other players. I want something similar to this game: ASYNC | The Backrooms - Roblox
This game’s developers have done an incredible work on coding the random generation.
To generate the Backrooms map randomly using chunks, you could try the following approach:
Determine the starting point: When the game starts or a player joins, you can generate the first chunk at a random position in the workspace, where the player will start. You can use the math.random() function to get a random position within a specific range.
Generate nearby chunks: Once the starting point is established, you can check if there are any existing chunks within a certain distance (e.g. NearbyChunkDistance). If there are no chunks nearby, generate new chunks around the starting point in a random pattern.
Add obstacles and walls: After generating the chunks, you can add obstacles and walls to make the environment look more like the Backrooms. You can use the Instance.new() function to create new instances of parts, and then add them to the chunk.
Update chunks: As the player moves around the map, you can check if they are close to any existing chunks. If they are, you don’t need to generate new chunks. Otherwise, you can generate new chunks around their current position, similar to step 2.
Here’s some sample code to get you started:
local ChunkSize = 200
local NearbyChunkDistance = ChunkSize * 2
-- Helper function to generate a new chunk
function GenerateChunk(position)
local chunk = Instance.new("Part")
chunk.Name = "Chunk"
chunk.Anchored = true
chunk.Position = position
chunk.Size = Vector3.new(ChunkSize, 2, ChunkSize)
chunk.Parent = workspace
-- TODO: Add obstacles and walls
return chunk
end
-- Keep track of all existing chunks
local existingChunks = {}
-- Function to generate new chunks around a given position
function GenerateNearbyChunks(position)
for i = 1, 5 do -- generate 5 new chunks
local x = position.X + math.random(-ChunkSize, ChunkSize)
local z = position.Z + math.random(-ChunkSize, ChunkSize)
local newPos = Vector3.new(x, position.Y, z)
-- Check if there are any existing chunks nearby
local nearbyChunk = nil
for _, chunk in pairs(existingChunks) do
if (chunk.Position - newPos).magnitude < NearbyChunkDistance then
nearbyChunk = chunk
break
end
end
-- Generate a new chunk if no nearby chunks found
if not nearbyChunk then
local newChunk = GenerateChunk(newPos)
table.insert(existingChunks, newChunk)
end
end
end
-- Function to check if player needs new chunks and generate them if needed
function UpdateChunks(player)
local currentPosition = player.Character.HumanoidRootPart.Position
-- Check if player is close to any existing chunks
local nearbyChunk = nil
for _, chunk in pairs(existingChunks) do
if (chunk.Position - currentPosition).magnitude < NearbyChunkDistance then
nearbyChunk = chunk
break
end
end
-- Generate new chunks if no nearby chunks found
if not nearbyChunk then
GenerateNearbyChunks(currentPosition)
end
end
-- Event handlers for player joining and moving
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
-- Generate the starting chunk at a random position
local startPos = Vector3.new(math.random(-500, 500), 0, math.random(-500, 500))
local startChunk = GenerateChunk(startPos)
table.insert(existingChunks, startChunk)
end)
end)
game:GetService("RunService").Heart
This is so complicated tbh, I want to know this first: How do I surround a chunk with 8 other chunks? Like this, the normally coloured carpet is the main chunk and the red ones are the surrounding chunks:
Like is there some kind of math formula for it possibly?
Knowing this first thing here will get me a lot more ahead.
The text you sent me was probably not made by you, but ChatGPT, I assume. So if you did use ChatGPT, then it makes stuff more confusing because it changed all of my code.