Greetings, today i'm trying to make a game similar to epic mining 2 and much other games similar to that. I'm trying to create the mine ore, dirt, stone etc generation within a certain position (scattered). The only thing is that I don't know how I can script it. I'm not begging or anything but i've started a little bit of the script. This may infact make me learn more about my lua knowledge and on mining games, probably going to make a tutorial how you can make one in the future.
local DirtLayer = {60, 199, 60} local DirtSize = {4,4,4} local StoneLayer = {60,200,60} local StoneSize = {4,4,4} local Lithosphere = {60,2200,60} local HotStone = {4,4,4} -- Requires Heat Protection aswell as O2. local Asthenosphere = {60,3300,60} local Magma = {4,4,4} local Underworld = {60,6660,60} local Hellstone = {4,4,4} --Ores local Coal = 1 -- Rarity local CoalSellValue = 22 local Copper = 1 -- Rarity local CopperSellValue = 29 local Lead = 1 -- Rarity local LeadSellValue = 35 local Nickel = 1.5 -- Rarity local NickelSellValue = 67 local Iron = 1.1 -- Rarity local IronSellValue = 67 local Silver = 1.6 -- Rarity local SilverSellValue = 156 local Gold = 2 -- Rarity local GoldSellValue = 322 local GenerationVectors -- Dirt,Stone,Coal,Copper etc local Coal2 = Instance.new("Part",workspace) Coal2.Size = Vector3.new(4,4,4) Coal2.Position = Vector3.new(math.random(#DirtLayer)) --I'm thinking it'd be somewhat like this.
To position a part somewhere at random, within a certain rectangular boundary, you could randomly set the x, y, and z component of its position like this:
Ore.Position = Vector3.new( math.random(leftBoundary,rightBoundary), math.random(bottomBoundary,topBoundary), math.random(frontBoundary,backBoundary) )
Now you mentioned that it's a mining game, so I imagine you'd need to generate the parts to be aligned to a grid. I'm going to assume that that grid size is 4 studs cube. Generally, you'd just have to multiply the position by your grid size, like so:
local gridSize = 4 Ore.Position = Vector3.new( gridSize * math.random(leftBoundary,rightBoundary), gridSize * math.random(bottomBoundary,topBoundary), gridSize * math.random(frontBoundary,backBoundary) )
Pretty basic, and you'll get parts positioned randomly within the rectangular region defined by the boundaries you'd set.
However, note that this does not check if there is already a part in a certain position, so it's possible to have multiple parts in one spot. This is great for rare ores, but not for regularly occurring ores that need to be placed in chunks, in veins, etc.
The solution for that is another system entirely. Here's how I assume the other games do it:
This system is also relatively memory efficient, because you're only generating parts when players break blocks, which is really the only time that they'd get access to those blocks. The mine size only gets bigger and bigger as time goes by. If you want, you could check when the number of blocks nears a certain threshold, then say "the mine will cave in after 40 more blocks are mined", and teleport/kill everyone while the parts are cleared and the mine is reset.
One caveat to this is that you're basically rolling a hypothetical n-sided, weighted dice every time a block is broken: there is no guarantee that you'll get a certain ore at a certain height. If you want that, what you could do is generate a list of ores at certain specific positions, and have your function check if it's already there instead of generating new ores to put in those places. You could create single ores or ore veins this way.
A more complex approach would be to use math.noise
, but that's kind of beyond what I could do and explain. Read more about how Minecraft does theirs.
Hope that helps!