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)
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.
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
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