I have the code
local button = script.Parent.Parent
local gui = script.Parent.Parent
-- This function generates a new Minecraft-style world
local function generateWorld()
-- Clear the current level
for _, child in pairs(game.Workspace:GetChildren()) do
if child.Name ~= "Terrain" and child.Name ~= "Camera" and (child:IsA("Model") and child:FindFirstChild("Humanoid") == nil) then
child:Destroy()
end
end
-- Seed the random number generator with the current time
math.randomseed(os.time())
-- Set the size of the chunks
local chunkSize = 32
-- Create a model to hold the chunks
local chunks = Instance.new("Model")
chunks.Name = "Chunks"
chunks.Parent = game.Workspace
-- Generate the chunks
local function generateChunk(x, z)
-- Create a 3D array to store the blocks in the chunk
local blocks = {}
for x = 1, chunkSize do
blocks[x] = {}
for y = 1, chunkSize do
blocks[x][y] = {}
for z = 1, chunkSize do
blocks[x][y][z] = 0
end
end
end
-- Generate the terrain
for x = 1, chunkSize do
for z = 1, chunkSize do
local height = math.random(1, chunkSize / 2)
for y = 1, height do
blocks[x][y][z] = 1
end
end
end
-- Generate caves
for i = 1, chunkSize do
local x = math.random(1, chunkSize)
local y = math.random(1, chunkSize)
local z = math.random(1, chunkSize)
local length = math.random(chunkSize / 4, chunkSize / 2)
local direction = Vector3.new(math.random(-1, 1), math.random(-1, 1), math.random(-1, 1))
for j = 1, length do
local pos = Vector3.new(x, y, z) + direction * j
if pos.X > 0 and pos.Y > 0 and pos.Z > 0 and pos.X <= chunkSize and pos.Y <= chunkSize and pos.Z <= chunkSize then
blocks[pos.X][pos.Y][pos.Z] = 0
end
end
-- Place the blocks in the chunk
for x = 1, chunkSize do
for y = 1, chunkSize do
for z = 1, chunkSize do
local block = Instance.new("Part")
block.Anchored = true
block.Size = Vector3.new(1, 1, 1)
block.CFrame = CFrame.new((x - 1) + (chunkSize * x), (y - 1) + (chunkSize * y), (z - 1) + (chunkSize * z))
if blocks[x][y][z] == 1 then
block.BrickColor = BrickColor.new("Dark green")
end
block.Parent = chunks
end
end
end
end
-- Generate the initial chunks around the player
local player = game.Players.LocalPlayer
local startPos = player.Character.HumanoidRootPart.Position
local startChunkCoords = Vector3.new(math.floor(startPos.X / chunkSize), math.floor(startPos.Y / chunkSize), math.floor(startPos.Z / chunkSize))
for x = startChunkCoords.X - 1, startChunkCoords.X + 1 do
for z = startChunkCoords.Z - 1, startChunkCoords.Z + 1 do
generateChunk(x, z)
end
end
-- Set up the player's movement
local mouse = player:GetMouse()
local camera = game.Workspace.CurrentCamera
local move = Vector3.new(0, 0, 0)
local keyboard = player:GetKeyboard()
local updateMovement = function()
-- Update the move vector based on keyboard input
move = Vector3.new(0, 0, 0)
if keyboard.W.Value then
move = move + camera.CFrame.lookVector
end
if keyboard.A.Value then
move = move - camera.CFrame.rightVector
end
if keyboard.S.Value then
move = move - camera.CFrame.lookVector
end
if keyboard.D.Value then
move = move + camera.CFrame.rightVector
end
move = move.Unit * 5
-- Update the player's position and camera
local newPos = player.Character.HumanoidRootPart.Position + move
player.Character:MoveTo(newPos)
camera.CFrame = CFrame.new(newPos, newPos + camera.CFrame.lookVector)
-- Load and unload chunks as necessary
local chunkCoords = Vector3.new(math.floor(newPos.X / chunkSize), math.floor(newPos.Y / chunkSize), math.floor(newPos.Z / chunkSize))
if chunkCoords ~= startChunkCoords then
-- Unload the old chunks
for x = startChunkCoords.X - 1, startChunkCoords.X + 1 do
for z = startChunkCoords.Z - 1, startChunkCoords.Z + 1 do
local chunk = chunks:FindFirstChild(x .. " " .. z)
if chunk ~= nil then
chunk:Destroy()
end
end
end
-- Load the new chunks
for x = chunkCoords.X - 1, chunkCoords.X + 1 do
for z = chunkCoords.Z - 1, chunkCoords.Z + 1 do
generateChunk(x, z)
end
end
-- Update the start chunk coords
startChunkCoords = chunkCoords
end
end
-- Set up the player's movement loop
local movementLoop = game:GetService("RunService").RenderStepped:Connect(updateMovement)
-- Set up the player's mouse look
local look = Vector2.new(0, 0)
local updateLook = function()
-- Update the look vector based on mouse input
local sensitivity = 0.005
local delta = mouse.Delta
look = look + Vector2.new(delta.X, delta.Y)
-- Clamp the look vector to a reasonable range
look = Vector2.new(math.clamp(look.X, -1, 1), math.clamp(look.Y, -1, 1))
-- Update the camera's CFrame based on the look vector
local newLook = camera.CFrame.lookVector
newLook = newLook:Lerp(Vector3.new(look.X, look.Y, newLook.Z), sensitivity)
camera.CFrame = CFrame.new(camera.CFrame.p, camera.CFrame.p + newLook)
end
-- Set up the player's mouse look loop
local lookLoop = game:GetService("RunService").RenderStepped:Connect(updateLook)
-- Set up the "Generate Level" button
button.MouseButton1Click:Connect(function()
-- Hide the main menu
gui.Visible = false
-- Generate the world
generateWorld()
end)
end
end
The problem I’m having is that for some reason the “generateWorld()” function isn’t being called correctly in the end. I made sure that the button and GUI is correct but when I do a print after “local function generateWorld()” then it doesn’t print when I press the button so I know its an error with the function being called incorrectly.