Infinite Mining Generation

This tutorial is going to cover the generation aspect of an infinite mining game, think The Quarry/Azure Mines/Mining Simulator, however this will not include randomized ores. This is for those who can learn a bit better through a tutorial than through analyzing source code I suppose.

Now then, let’s start fresh in a new place by removing the baseplate.

First thing we are going to want to do is define how our blocks are going to generate, and to begin we want a perfect cube. Insert a part, anchor it, and give it a size of 6, 6, 6. Let’s also make a new folder named “Ores” in ServerStorage and a folder named “Cubes” in Workspace. Lets put the cube inside of the “Cubes” folder.
image

We also want to make sure this part is positioned at the 0, 0, 0 origin, as that can simplify things in the future.

What we need to do first is visualize how the blocks will generate, and in this scenario we can use 6 cubes, one on each side of the original cube with the corners being left open.

Now’s the time where we can open our first script, lets insert a generic Script into ServerScriptService
image

The best method in my experience is transcribing these “generation positions” in a table, then referencing that table whenever we need to generate a part. Since we already have these “generate positions” defined and the original cube is positioned at 0, 0, 0, it’s very simple to copy them into the script.

local positions = {
	Vector3.new(6,0,0);
	Vector3.new(-6,0,0);
	Vector3.new(0,6,0);
	Vector3.new(0,-6,0);
	Vector3.new(0,0,6);
	Vector3.new(0,0,-6);
}

Now, let’s look at the actual generation itself. Whenever a block is mined, we can give that block’s position to a generation function, where we loop through our positions table and add them to the block’s position.

function Generate(pos)
	if pos then
		for _,pos2 in pairs(positions) do
			local newPos = pos+pos2
			local ore = game.ServerStorage.Ores.Part:Clone()
			ore.Position = newPos
			ore.Parent = workspace
		end
	end
end

Now an issue that some of the more astute among you may have picked up on, this system would cause parts to generate over each other and generate in spots where blocks have already been mined. I shall rectify the situation.

Let’s make another folder inside of the “Cubes” folder, we can name it “Generated”.
image

Now, whenever an ore generates we can add a Vector3 value to this “Generated” folder and cross-reference that with other ores that generate.

function Generate(pos)
	if pos then
		for _,pos2 in pairs(positions) do
			local newPos = pos+pos2
			local previouslyGenerated
			for _,generated in pairs(workspace.Cubes.Generated:GetChildren()) do
				if generated.Value == newPos then
					previouslyGenerated = true
				end
			end
			if newPos.Y > 0 then
				previouslyGenerated = true
			end
			if not previouslyGenerated then
				local ore = game.ServerStorage.Ores.Part:Clone()
				ore.Position = newPos
				ore.Parent = workspace.Cubes
				local val = Instance.new("Vector3Value")
				val.Value = newPos
				val.Parent = workspace.Cubes.Generated
			end
		end
	end
end

Since we aren’t generating the first layer of our mine when the game starts, instead having them already manually placed, we also need to add those first layer cube’s positions to the “Generated” folder.

for i,cube in pairs(workspace.Cubes:GetChildren()) do
	if cube:IsA("Part") then
		local val = Instance.new("Vector3Value")
		val.Value = cube.Position
		val.Parent = workspace.Cubes.Generated
	end
end

Now to move past this point, let’s give our cube a click detector that we can utilize to detect when it is mined. We can connect to this using a generic for loop we created before, and then connecting to a new one each time a child is added which we will do next. This is also the point where we should make a clone of our starting cube and move it into the “Ores” folder. All we need to do is connect the cube being clicked to the generate function and that’s a sealed deal.

for i,cube in pairs(workspace.Cubes:GetChildren()) do
	if cube:IsA("Part") then
		local val = Instance.new("Vector3Value")
		val.Value = cube.Position
		val.Parent = workspace.Cubes.Generated
		cube.ClickDetector.MouseClick:Connect(function()
			Generate(cube.Position)
			cube:Destroy()
		end)
	end
end

And connecting the ClickDetector of a fresh ore when it is generated:

function Generate(pos)
	if pos then
		for _,pos2 in pairs(positions) do
			local newPos = pos+pos2
			local previouslyGenerated
			for _,generated in pairs(workspace.Cubes.Generated:GetChildren()) do
				if generated.Value == newPos then
					previouslyGenerated = true
				end
			end
			if newPos.Y > 0 then
				previouslyGenerated = true
			end
			if not previouslyGenerated then
				local ore = game.ServerStorage.Ores.Part:Clone()
				ore.Position = newPos
				ore.Parent = workspace.Cubes
				ore.ClickDetector.MouseClick:Connect(function()
					Generate(ore.Position)
					ore:Destroy()
				end)
				local val = Instance.new("Vector3Value")
				val.Value = newPos
				val.Parent = workspace.Cubes.Generated
			end
		end
	end
end

Finished product:

Place file:
MiningTutorial.rbxl

85 Likes

It worked!


(I don’t have a screen recorder right now, so i decided to take a screenshot instead.)
amazing tutorial! learned a lot. :smiley:

12 Likes

Awesome! Thanks, dude! Might use this in the future. :smiley:

3 Likes

Wow! This is awesome! I’ve been considering making a mining game and this will help a lot!

4 Likes

Glad I could be of assistance!

I am pondering continuing to add on to this system in a sort of “tutorial series”, fleshing out smaller tutorials for things like adding varying ores, setting layers, unbreakable blocks, and other things of that nature, though I’m not sure how much that would help people who have followed this tutorial in particular.

6 Likes

It would help me for sure :joy:
Mining Games have lots of potential and this tutorial could make people a lot of money if the toght developer followed these steps and used this in the correct way - I and many other people really appreciate you sharing this amazing creation with the public. Thank you!

2 Likes

An extremely amazing tutorial. But…

I’m not really experienced at this. What does “open corners” mean?

1 Like

No blocks on the corners. It wouldn’t be seen anyway, so keeping the corners empty save on processing.

2 Likes

Is it possible to get this working with an infinite terrain generator using chunk loading stuff? (Kind of like minecraft)

I have tried using this for generating the terrain

And this for making the chunks

But I couldn’t get anything to work

1 Like

No, this system could not be implemented to generate chunks similar to Minecraft, and I’m not aware of any real resources directly pertaining to ROBLOX that can be used to attain this feature.

There’s a good video by Sam Hogan where he briefly touches on how to make terrain generation similar to Minecraft, I definitely recommend checking it out.

3 Likes

Hello, nice tutorial! Very basic and generally the beginning steps to creating a mining game on Roblox.
Similar to The Quarry by Dummiez (a classic mining game that once had a Roblox event. I use to be a moderator for his game few years back. Good times! :smile:)

I have dabbled in mining generation like this, and I would like to point out it is better to store the data for the old blocks that were mined, into ServerStorage, as you dont need all those Vector3’s being replicated to everyone’s client.

Also I would like to mention that you are all free to use an open source I made a while back, that takes this even further with ore generation, inventory display, and other fun gui work. Open Source Mining Game - Roblox
Please note that at the time of this post (since I might rescript this in the future), the game is using very old code back when I was still learning how to optimize well. I would recommend not using this as a base, but instead learn from it and rescript it entirely. :sunglasses:

2 Likes

The method I used here is not my typical method.

I typically use an array, turning the Vector3 to a string and using it as the index and setting the value to the ore.

I figured that having it as a visible folder would be more understandable for less adept scripters, though I never considered storing them in ServerStorage over storing them in the Workspace.

1 Like

There is an open-source cave generation mining game if this helps you:

1 Like

I’ve made one of these systems before around half a year ago, so the tutorial itself was not of use to me, however

One thing I got stuck on to the point I quit working on the system was the generation of realistic caves, I see you plan to expand the series, could this be one of three things you cover?

It would also be cool if you could just cover how to generate caves from their edges rather then having to generate a cave then it’s outline

Looking into cellular automata algorithm might give you insight. It tends to be good at helping generate good caves.
The core of random generation is acting based on the positional values of the block.

2 Likes

Coming from a person who created a large scale infinite mining game, I have a few concerns with this:

  • The way you’re checking if something already exists at a certain point uses getchildren, meaning that it will take longer to check this data every time new blocks are generated. I recommend using a small region3 check instead with no whitelist or blacklist at that position for maximum performance.
  • Clickdetectors aren’t necessary, you’ll be spawning thousands of new objects and making a bunch of connections to do this, all which could just be done with a renderstep function on the mouse that checks the mouse target.
  • Naturally in an infinite space, without bounds, there will be a point where you start to have graphical issues due to anything from being too far out or just having too many objects on screen. You’re also gonna have crazy memory issues because of how many objects are being spawned and deleted, Roblox is not good at handling this. You need some kind of block cache system that reuses blocks instead of deleting them, otherwise the game will crash due to extreme memory leaks.
8 Likes

making this into minecraft would require perlin noise am i right? can you do a tutorial on perlin noise please

I’ve tried making this use perlin noise terrain and it worked but the blocks past y0 go up and stuff instead of the game knowing its the top layer and i dont know how to do that does anyone know

1 Like