can u make a roblox script that makes an infinitely generated skyscraper that keeps going up
Assistant
Certainly! Below is a Roblox script that generates an infinitely tall skyscraper by creating new floors as the player ascends. This dynamic generation ensures that the skyscraper extends upward infinitely without causing significant performance issues.
Script: Infinite Skyscraper Generator
Instructions:
Open Roblox Studio and create a new place or open your existing place.
In the Explorer window, click on the Workspace.
Click + to add a new object and select Script to create a new script inside the Workspace.
Rename the script to InfiniteSkyscraper for clarity (optional).
Copy and paste the following code into the script.
Run the game to see the infinite skyscraper in action.
lua
-- Infinite Skyscraper Generator Script
-- Configuration
local floorHeight = 10 -- Height of each floor
local floorSize = Vector3.new(50, floorHeight, 50) -- Size of each floor (Width, Height, Depth)
local floorsAhead = 5 -- Number of floors to generate ahead of the player's current floor
local floorsBehind = 5 -- Number of floors to keep below the player's current floor
-- Variables
local floors = {} -- Table to store generated floors
local highestFloorNumber = 0 -- Keeps track of the highest floor number generated
local lowestFloorNumber = 1 -- Keeps track of the lowest floor number generated
-- Services
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
-- Function to create a floor
local function createFloor(floorNumber)
local floor = Instance.new("Part")
floor.Name = "Floor_" .. floorNumber
floor.Size = floorSize
floor.Position = Vector3.new(0, (floorNumber - 1) * floorHeight, 0)
floor.Anchored = true
floor.Parent = workspace
floors[floorNumber] = floor
end
-- Function to remove a floor
local function removeFloor(floorNumber)
if floors[floorNumber] then
floors[floorNumber]:Destroy()
floors[floorNumber] = nil
end
end
-- Initialize initial floors
for i = 1, floorsAhead + 1 do
createFloor(i)
highestFloorNumber = i
end
-- Main loop to generate floors as the player ascends
RunService.Heartbeat:Connect(function()
for _, player in pairs(Players:GetPlayers()) do
local character = player.Character
if character and character:FindFirstChild("HumanoidRootPart") then
local playerY = character.HumanoidRootPart.Position.Y
local currentFloorNumber = math.floor(playerY / floorHeight) + 1
-- Generate floors ahead
while highestFloorNumber < currentFloorNumber + floorsAhead do
highestFloorNumber = highestFloorNumber + 1
createFloor(highestFloorNumber)
end
-- Remove floors behind
while lowestFloorNumber < currentFloorNumber - floorsBehind do
removeFloor(lowestFloorNumber)
lowestFloorNumber = lowestFloorNumber + 1
end
end
end
end)
How the Script Works
Configuration Variables:
floorHeight: The vertical height of each floor.
floorSize: The dimensions of each floor (width, height, depth).
floorsAhead: Number of floors to generate ahead of the player to ensure seamless ascent.
floorsBehind: Number of floors to retain below the player to conserve memory.
Variables:
floors: A table to keep track of all the floors that have been generated.
highestFloorNumber: The number of the highest floor that has been generated so far.
lowestFloorNumber: The number of the lowest floor that still exists in the game.
Services:
Players: Service to access all players in the game.
RunService: Service to handle game loop events, such as Heartbeat.
Functions:
createFloor(floorNumber): Generates a new floor with the given floor number.
Creates a new Part.
Sets its size and position based on the floor number.
Anchors it so it doesn't fall due to gravity.
Parents it to the Workspace.
Stores it in the floors table.
removeFloor(floorNumber): Removes a floor when it's no longer needed.
Destroys the Part.
Removes it from the floors table.
Initialization:
The script generates an initial set of floors (from floor 1 to floorsAhead + 1) to start the skyscraper.
Main Loop:
Uses RunService.Heartbeat to run a function every frame.
For each player in the game:
Checks if the player's character and HumanoidRootPart exist.
Calculates the currentFloorNumber based on the player's vertical position.
Generate Floors Ahead:
While the highestFloorNumber is less than , it generates new floors ahead of the player.
Notes
Performance Optimization:
The script only generates floors as needed and removes floors that are far below the player.
This ensures that the game doesn't lag due to an excessive number of parts.
Customization:
You can adjust the floorHeight, floorSize, floorsAhead, and floorsBehind variables to change the appearance and performance of the skyscraper.
To add more features to each floor (e.g., walls, decorations), you can modify the createFloor function to include additional parts or models.
Safety Checks:
The script includes checks to ensure that the player's character and HumanoidRootPart exist before attempting to calculate positions.
Testing the Script
Run the Game:
Click the Play button to start the game.
As your character ascends (you may need to use tools like jumping or flying), you will notice that new floors are generated above you.
Floors far below you are removed to keep the game's performance optimal.
Troubleshooting
Player Cannot Ascend:
Ensure there is a method for your character to move upward. You might need to add stairs, ladders, or tools that allow vertical movement.
Floors Not Generating:
Check the Output window in Roblox Studio for any error messages.
Make sure the script is correctly placed inside the Workspace and that there are no typos.
Enhancements
Adding Stairs or Elevators:
To make the skyscraper functional, consider adding stairs or elevators in the createFloor function.
You can create a Model for each floor that includes these features.
Visual Variety:
Randomize floor designs to add variety.
Use different colors or materials for each floor.
Multiplayer Considerations:
The script supports multiple players by generating floors based on each player's position.
Ensure that any added features are compatible with multiplayer gameplay.
Feel free to customize the script further to suit your game's needs. If you have any questions or need assistance with modifications, don't hesitate to ask!
Dataset releases
Not yet included in a published release.
currentFloorNumber + floorsAhead
Remove Floors Behind:
While the lowestFloorNumber is less than currentFloorNumber - floorsBehind, it removes floors that are far below the player to optimize performance.