Help with mining system

So I have a pickaxe system that works but the problem is when the player to too far from the minable block, the player can still break it. I need help preventing that.

Client:

local breakRange = 18

RunService.Heartbeat:Connect(function()
	local mouseTarget = mouse.Target
	local isMouseTargetValid = false

	for i,v in ipairs(BreakableBlocks) do
		if mouseTarget == v and not newBreakSelectionBox then
			local distance = (v.Position - character.PrimaryPart.Position).Magnitude
			
			print(distance)

			if distance <= breakRange then
				selectedBlock = v
				newBreakSelectionBox = BreakSelectionBox:Clone()
				newBreakSelectionBox.Adornee = v
				newBreakSelectionBox.Visible = true
				newBreakSelectionBox.Parent = v
				isMouseTargetValid = true
			end
		else
			mouse.TargetFilter = mouseTarget
		end
	end

	if not isMouseTargetValid and mouseTarget ~= selectedBlock or mouseTarget == nil then
		removeBreakSelectionBox()
		selectedBlock = nil
	end
end)
1 Like

Could you send a video of you testing it, also have your console open. And print the var “distance”
It would help me to be able to debug what is happening.

Write an else clause after if distance <= breakRange then

if distance <= breakRange then
   -- your current code
else
  selectedBlock = nil
  removeBreakSelectionBox()
end

I have tried that but that doesn’t work.

Can you show the code of the method removeBreakSelectionBox?

local function removeBreakSelectionBox()
	if newBreakSelectionBox then
		newBreakSelectionBox:Destroy()
		newBreakSelectionBox = nil
	end
end

Try removing this.

and mouseTarget ~= selectedBlock 

It works but it keeps destroying and creating a new box every heartbeat.

I see why that’s happening. Instead do the distance check before checking if there’s a break selection box in the for loop.

for i, v in ipairs(BreakableBlocks) do
  if mouseTarget == v then
    local distance = (v.Position - character.PrimaryPart.Position).Magnitude
    if distance > breakRange then continue end
    isMouseTargetValid = true
    selectedBlock = v

    if not newBreakSelectionBox then
      newBreakSelectionBox = BreakSelectionBox:Clone()
      -- set properties for the break selection box
    end
  else
    mouse.TargetFilter = mouseTarget
  end
end

if not isMouseTargetValid or not mouseTarget then
  removeBreakSelectionBox()
  selectedBlock = nil
end

Thanks! But now you can’t switch to a different block to mine.

Good catch! I am writing this pretty hurriedly.

for i, v in ipairs(BreakableBlocks) do
  if mouseTarget == v then
    local distance = (v.Position - character.PrimaryPart.Position).Magnitude
    if distance > breakRange then continue end
    isMouseTargetValid = true

    if selectedBlock ~= v then
     removeBreakSelectionBox()
    end

    selectedBlock = v

    if not newBreakSelectionBox then
      newBreakSelectionBox = BreakSelectionBox:Clone()
      -- set properties for the break selection box
    end
  else
    mouse.TargetFilter = mouseTarget
  end
end

if not isMouseTargetValid or not mouseTarget then
  removeBreakSelectionBox()
  selectedBlock = nil
end
2 Likes

Thanks, man for helping him out, I had to jump over and help some other guy with ProcessReciept.

Oh I know which one you’re talkin about haha, I was going to go to that one but then saw this one.

Glad it worked out. We both got to help people out.

The mouse filter doesn’t seem to work.
mouse.TargetFilter = mouseTarget


I’m trying to filter everything except for the BreakableBlocks.

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