Need help with a Pickaxe mining system

Hello devs! I was trying to build a Mining System in roblox today. And I am running into some errors.

The problems I’m facing-

  • There is uncertainty in mining the ores. I do not understand why.
  • I would like to make a collecting system wherein I want to collect the ores there, as tools on my hotbar.

Here are the scripts I used.
LocalScript for the tool

local tool = script.Parent
local anim = tool:WaitForChild("Animation")
local se = tool.Pickaxe.SoundEffect
 
local plr
local char 
local track
local connection
local mouse
local canMine=true
local stringVal

local function onActivated()
	local part = mouse.Target
	if part and part.Parent.Name == "Deposits" and canMine then
		local hrp = char:WaitForChild("HumanoidRootPart")
		local dist = (hrp.CFrame.Position - part.Position).Magnitude
		
		if dist<10 then
			canMine=false
			hrp.Anchored = true
			track:Play()
			track.KeyframeReached:Connect(function(kfName)
				if kfName == "Hit" then
					tool.SoundEvent:FireServer()
				end
			end)
			if part.Parent.MineType.Value == "Stone" then
				task.wait(3)
			end
			if part.Parent.MineType.Value == "Gold" then
				task.wait(9)
			end
			if part.Parent.MineType.Value == "Iron" then
				task.wait(5)
			end
			if part.Parent.MineType.Value == "Bronze" then
				task.wait(7)
			end
			tool.CollectEvent:FireServer(part, part.Parent.MineType.Value)
			canMine=true
			hrp.Anchored=false
			track:Stop()
		end
	end
end

tool.Equipped:Connect(function()
	plr = game.Players.LocalPlayer
	char = plr.Character or plr.CharacterAdded:Wait()
	mouse = plr:GetMouse()
	local hum = char:WaitForChild("Humanoid")
	track = hum.Animator:LoadAnimation(anim)
	connection = tool.Activated:Connect(onActivated)
end)

tool.Unequipped:Connect(function()
	plr = nil
	char = nil
	connection:Disconnect()
end)

ServerScript to collect-

local tool = script.Parent
local SoundEvent = tool:WaitForChild("SoundEvent")
local SoundEffect = tool.Pickaxe:WaitForChild("SoundEffect")
local CollectEvent = tool:WaitForChild("CollectEvent")

local r = Random.new()
min = 1
max = 2

SoundEvent.OnServerEvent:Connect(function(plr)
	SoundEffect:Play()
end)

CollectEvent.OnServerEvent:Connect(function(plr, part)
	part.Transparency = 1
end)

ServerScript in the model where the ores are-

local deposits = script.Parent:GetChildren()
local mineTypeTemp = deposits.Minetype

for _,v in pairs(deposits) do
	local mineType = mineTypeTemp:Clone()
	mineType.Parent = v
	v:GetPropertyChangedSignal("Transparency"):Connect(function()
		if v.Transparency == 1 then
			v.CanQuery = false
			task.wait(15)
			v.Transparency = 0
			v.CanQuery = true
		end
	end)
end

Here is the proper breakup of my Explorer if you are still confused-
image
image
These are the tools, which I want to add to the player’s hotbar after mining the ores.
image

Thank you so much for reading this. Please provide me with a solution. Thanks :slight_smile:

2 Likes

What is your problem in the script? Are you trying to make when collected the ores go into your hot bar?

One thing i did notice however is that you have spaghetti code, which is a bad practice, (this is not the problem however) so to avoid that bit you would want to make a table like this

local oresWaitTime = {
["Stone"] = 3,
["Bronze"] = 9,
-- and so on E.x the rest of the ore values
}

The remove the big if statement for waiting and change it to

task.wait(oresWaitTime[part.Parent.MineType.Value])
-- so on with the rest of the script

im sorry that took so long i couldn’t write it all out im writing this on a phone.

2 Likes

Looks liike you’re already passing the ore’s value in the remote:

tool.CollectEvent:FireServer(part, part.Parent.MineType.Value)

I’m assuming MineType is a StringValue and that your tools named after ores are stored under an object named “Ores” with the path as: game.ServerStorage.Ores

CollectEvent.OnServerEvent:Connect(function(plr:Player, part:Basepart, value:string)
	-- Handle mining
	part.Transparency = 1
	part.CanQuery = false
	task.delay(15, function() part.Transparency = 0 v.CanQuery = true end)
	-- Add tool
	local oreTool:Tool = game.ServerStorage.Ores[value]:Clone()
	oreTool.Parent = player.Backpack
end)

Can change the other script to:

for _,v in pairs(deposits) do
	local mineType = mineTypeTemp:Clone()
	mineType.Parent = v
end
2 Likes

Hi, I am relatively new to scripting. Thank you so much for taking time and trying to improve my code! I really appreciate it.

Thank you so much. It worked. I cannot thank you enough. Could you please explain the meaning of colon? Is colon used to define a variable in a more specified manner?

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