Random generation when mining

im trying to make it so when the player mines a block, there is a chance itll spawn something rare (think like rex:r or untitled mining game, if you havent played those then go play them to see what im referring to if you dont know)

the problem im having is i cant actually pull the rarity and the actual block itself at the same time, the original script i found from a tutorial (cause im making this game for fun and i dont wanna put too much time in it) only allows for one single block to be generated.

i have looked through multiple devforum posts and youtube videos, ive tried mixing two scripts together but im not the greatest at scripting especially when it comes to mixing two scripts together, so im struggling to make it actually work

this is the main script i have

local loottable = require(game.ServerStorage:WaitForChild("LootTable"))
print(loottable)
local ores = game.ServerStorage.Ores
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);
}

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
				function GetRandomOre()
					local Sum=0
					for OreName,Chance in pairs(loottable) do
						Sum += Chance
					end
					local RandomNumber = math.random(Sum)
					for OreName,Chance in pairs(loottable) do
						if RandomNumber <= Chance then
							return OreName
						else
							RandomNumber -= Chance
						end
						end
					end
				local orerarity = loottable
				local ore = game.ServerStorage.Ores.banana: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

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

this is my loottable

module.OreRarities {
	["grass"] = 1,
	["banana"] = 99,
}

module.Ores {
	["grass"] = {
		game.ServerStorage.Ores.grass
	},
	["banana"] = {
		game.ServerStorage.Ores.banana
	},
}
1 Like

You have hard coded it to pick the banana block instead of the grass block, so where you generate the pre by getting it from server storage just replace banana with
[GetRandomOre()] that just basically means that it should get the ore which the random function would return

1 Like

yeah i realized that, i did get a solution from someone else so i’ll put it here for anyone else wondering ! ! !

first you have to have a loottable in a module script, like:

return {
    ["red"] = 5,
    ["orange"] = 20
}

then you’ll grab the module script in your main script, basically just local name = require

theres a whole mining system in the devforums somewhere, i’ll reply once i find it (or you can copy my script, but the post is more indepth about the mining system then i am)

you’ll want to have a table in the main script for the directory of the ores, like:

local Ores = {
     ["red"] = game.ServerStorage.YourFolderName.red,
     ["orange"] = game.ServerStorage.YourFolderName.orange
}

then to get the actual chance and stuff working, you’ll use the GetRandomOre() function i have in my script like

local ore = Ores[GetRandomOre()]:Clone()

i know this isnt the greatest explanation, but its an explanation.
how GetRandomOre() works is the chance of the ore is basically just: Chance/Sum of All Chances
so for our example, red is a 5/25 or a 1/5 chance.

thats all, could probably go more in depth but im not the greatest scripter and still dont understand for i_ in pairs or whatever that is

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.