How do I make regenerating boulders?

So, I am making a game all about climbing up a mountain (or walking up it). And there is this one obstacle where I want boulders to roll down a slope and try to kill you. The thing is, I don’t know how to make it spontaneous. I know the kill script though. If anyone can help me, please reply to this post. Have a nice day :happy2:

Ps: I have no idea if this sort of topic is on the internet. If it is, you can link me a video.

2 Likes

One solution is a (sorry to the people who hate this kind of method) while loop.
A basic example would be

local SpawnBoulders = true

-- Create a coroutine (or do any other method you know) to prevent any code past this to not execute
coroutine.wrap(function()
-- Repeat this function as long as SpawnBoulders is true
while SpawnBoulders do
    -- Get random Vector3 for the Boulder to spawn at
    local RandomPosition = Vector3.new(math.random(0, 10), 10, 0)
    -- Clone the existing Boulder Template
    local NewBoulder = Boulder:Clone()
    -- Set the Position (Or CFrame if you want) of the Boulder and parent it
    NewBoulder.Position = RandomPosition
    NewBoulder.Parent = workspace
    -- If you want the Boulder to get destroyed after X seconds, use Debris
    game:GetService("Debris"):AddItem(NewBoulder, 10) -- Destroys after 10 seconds
    wait(5) -- Every 5 seconds
end
end)() -- The () at the end here execute the coroutine

You will need to edit this example to work with your game.

3 Likes

You don’t have to use a while loop, actually, for your solution to work. You can use the same code and use RunService.Heartbeat, which runs at 60 fps usually. All you need is an if SpawnBoulders then condition and you can get the same thing you get with a while loop :wink:

My bad, I forgot to add a wait() inside the code and only just now realized it thanks to you.

1 Like

Using Heartbeat would make the code run way more than it needs to; you don’t need to be checking if boulders can spawn every frame. It will be way more impactful than using a while loop.

1 Like

Yes but it’s not hard to buffer if you want it to run it less often. All you need is a counter and another condition if math.mod(counter, 3) == 0 then if you want it to run every 3 frames, for example. Using Heartbeat isn’t a liability, it is more consistent than using a while wait loop.

Why would you go through the extra steps to buffer to make it run less often? You’ll still be doing more than just having a while loop because it would still check every frame whether or not a new boulder should be spawned. A while loop is a better solution.

1 Like

Adding an extra check isn’t really that bad, and the reason I am bringing up Heartbeat is because of this:

Wait is inconsistent - there are multiple posts regarding this; likewise, I feel like it is beneficial to give more than one way to do things to solve the same problem. I’m not saying one solution is better than the other - each solution has it’s benefits.

Thank you for all the help :happy2:

Do you still need help? @Dev_BB1

Not really, but thanks for asking :happy2: You can still help.

No, there are many posts about why you should avoid using wait when you don’t need to. Wait in itself isn’t inconsistent except for the fact that it doesn’t always wait the exact amount of time that you specify; for a use like this using a loop with wait is more than perfect and less impactful than using Heartbeat. Heartbeat would be better if you need precision, but waiting for boulders to spawn is not something that you need precision for.

1 Like