hey, I just started scripting on lua. I am making a game with my friend, where you walk down a bunch of hallways kind of like doors if you've heard of that game. But i kinda don't know how to code it so it spawns in new chunks. My idea is i made a bunch of different hallways, and I want them to spawn randomly. from now on i'll call the hallways chunks. And I wanted it so that there is only about 7 chunks spawned in at once before chunks behind you start disapearing. obviously every time you walk in a new chunk, another chunk spawns in 7 chunks infront of you. I also want the first chunk to always be the same room. I know its a lot but does anyone know how to code anything like this?
#need help with code
9 messages · Page 1 of 1 (latest)
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local chunksFolder = game.Workspace.Chunks
local loadedChunks = {}
local maxChunks = 7
local currentPosition = 0
-- Load the initial chunk
if #loadedChunks == 0 then
local firstChunk = chunksFolder.FirstChunk:Clone()
firstChunk.Parent = game.Workspace
firstChunk.Position = Vector3.new(0, 0, 0)
table.insert(loadedChunks, firstChunk)
end
local function loadChunks()
for _, chunk in ipairs(loadedChunks) do
if chunk.Position.Z < currentPosition - 50 then
chunk:Destroy()
end
end
while #loadedChunks < maxChunks do
local newChunk = chunksFolder:GetChildren()[math.random(1, #chunksFolder:GetChildren())]:Clone()
newChunk.Parent = game.Workspace
newChunk.Position = Vector3.new(0, 0, currentPosition + 50)
table.insert(loadedChunks, newChunk)
end
end
local RunService = game:GetService("RunService")
RunService.Heartbeat:Connect(function()
if character and character.PrimaryPart then
currentPosition = character.PrimaryPart.Position.Z
loadChunks()
end
end)
make sure you’ve created all the different hallway models (chunks) you want to use in your game. It’s helpful to organize these by placing them in a folder called Chunks in Roblox Studio. This way, you can easily access them later on. You’ll want to create a LocalScript in either StarterPlayerScripts or StarterCharacterScripts. This script will manage the chunk spawning as the player moves around.
I'll see if this works
hi
@blazing solar yeah I might just be stupid but it doesn't seem to be working
** You are now Level 1! **