Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I am making a mining game but I cant figure out how to mine a block below me! How do I do this?

What I need: I am using OnTouch as my way to break blocks, and using the classic ROBLOX slash animation, but I want to hover over a block with the pickaxe tool, and whenever the tool is activated, the block will lose health. I am also using a GUI that is shown to the player whenever they hit the block. This GUI shows the rock health. All I need is a way to hover over a block in-range and be able to break it when my pickaxe is activated. Any help would be appreciated!

Tool Code

local tool = script.Parent

local function mine()
	local str = Instance.new("StringValue")
	str.Name = "toolanim"
	str.Value = "Slash"
	str.Parent = tool
	tool.Mining.Value = true
	wait(0.5)
	tool.Mining.Value = false
end

tool.Activated:Connect(mine)

Mineable Rock Code

local rock = script.Parent
local swingsRemaining = 10
local rockpos = script.Parent.Position

local rs = game:GetService('ReplicatedStorage')
local ss = game:GetService('ServerStorage')
local ores = rs:WaitForChild('Ores'):GetChildren()
local once = true
local i = ''
local function onTouch(op)
	local tool = op.Parent
	if tool:IsA('Tool') and tool.Mining.Value == true then
		if once == true then
			local label = game:GetService('ServerStorage').HealthScreen:Clone()
			local plrpress = game:GetService('Players'):GetPlayerFromCharacter(op.Parent.Parent)
			label.Parent = plrpress.PlayerGui
		end
		once = false
		script.Hit:Play()
		swingsRemaining-=1
		game:GetService('Players'):GetPlayerFromCharacter(op.Parent.Parent).PlayerGui.HealthScreen.Health.Green.Size = UDim2.new(swingsRemaining/10, 0,0.5,0)
	end
	if swingsRemaining <= 0 then
		dropOres(math.random(1,3))
		rock:Destroy()
	end
end

function dropOres(amount)
	for i = 1,amount do
		local rndmOre = ores[math.random(1,#ores)]:Clone()
		local cs = ss:WaitForChild('Collect'):Clone()
		rndmOre.Parent = workspace
		rndmOre.Position = rock.Position
		cs.Parent = rndmOre
	end
end
rock.Touched:Connect(onTouch)

2 answers

You could use player.Mouse().Target to find the block the player's mouse is above. To find if the player is within a distance of the block you can subtract the position of the player and the block and add .Magnitude

This discussion is preserved as it was. New answers, comments, and votes are closed.