How do I create infinite stairs?

Hallo, I’m working on one of those “Climb _____ stairs to winners!” games, instead of creating stairs manually I want to create a script that loads them in automatically, this not only makes it much easier to create the game but also makes the game more enjoyable and makes it more challenging to be first, instead of it being a battle of who played the game first it will be a battle of who will be motivated enough to be the highest player.

1 Like

You could make an infinite loop to create new parts using Instance.new and positioning the new part to be the last part’s position + Vector3.new(0,1,1)

1 Like

When I tried doing that Roblox Studio almost crashed and it showed me error message “Script timeout: exhausted allowed execution time”.

It sounds like you’re using a while true do loop, switch it to while wait() do loop instead.

1 Like

I tried that too but nothing happened.

Here’s the code for the block:

local Stair = script.Parent

while wait() do
local Block = Instance.new("Part")
Block.Size = Stair.Size
Block.BrickColor = BrickColor.Random()
Block.Position = Stair.Position + Vector3.new(0,1,1)
end

You’re generating new parts in that script, but it’s setting the position of the part to the same position every time. You need to set the position of the new part to the previous part’s position + Vector3.new(0,1,1).

1 Like

Procedural generation is probably the best option, although you could simply generate the parts every tick.

local _tick = 1;
local y = 1;

while (true) do
    task.wait(_tick);
    local newPart = Instance.new("Part");
    newPart.Position = Vector3.new(1, y, 1);
    newPart.Parent = workspace;
    y += 1;
end
-- Code was not tested.
2 Likes

Firstly,

Avoid while wait() do, both because wait() will eventually be deprecated, and also because it’s generally bad code practice making your code less readable. Also, while true do and while wait do are the same conditional loop called a while loop.


OP, what you’re going to run into using a traditional loop is that eventually your game’s speed will slow down. You would see more success from generating stairs based on the current highest y-value of a HumanoidRootPart in the server. This is much more practical and it will only generate as far as the most dedicated player is willing to climb.

To do this you would track positions of players in your game, or at least of active Humanoids, and then only generate to the player who is the highest up (+10-15 extra steps to avoid latency problems and falling).

1 Like

How would I track the player’s y-position?

So, you want the stairs to keep going as long as the players will? And whoever goes higher wins(for now). Till someone new goes higher?

You’ll have to generate them based on the player’s position. Since you can’t generate infinity stairs, you’ll just keep generating them till they give up, or their internet goes down.

I think you could just monitor all players’ Y positions, and when they get within ‘x’ studs of the current stair height, generate another stair, and so on. (you could even create enough ahead to where they can’t see the end, in fog maybe).

Not sure how i’d go about coding that, just an idea of how I might approach the problem.

2 Likes

Here is the gist of what you would need to do to handle this as optimally as possible:

  • CharacterCreated event inserts Position vector per player into a dictionary under their index (an array would work as well, it would just yield less information).
  • CharacterRemoving event clears player’s dictionary index entirely (to avoid problems with nil, or you could write __index to your table).
  • Assign a metatable to your dictionary to act like a Changed event for it using __index and __pairs (this is necessary because a constant loop iterating this player dictionary will eat more memory than a metatable would, I assume), provided that the new highest Vector.Y is larger than the previous highest Y-Value in that server, generate stairs upwards of the previous highest Y-Value by an increment of your decision.

The metatable is only necessary if you are absolutely worried about performance, otherwise a loop can work as well, because you will still avoid constant Instance creation.

@Sir_Highness is generally correct in his answer.

1 Like