Help with mining system

I assume the target you’re hovering over is a model? In that case mouse.Target might be returning a part within the model which would result in the CheckIfMinable function failing.

Can you tell me what BreakableBlocks is? Is it a table or a folder in workspace that you’re using :GetChildren on?

BreakableBlocks are tags.
image

The target might be getting the other parts. You could try mouse.Target.Parent

New code:

RunService.RenderStepped:Connect(function()
	local mouseTarget = mouse.Target
	local isMouseTargetValid = false
	
	for i,v in ipairs(BreakableBlocks) do
		if v ~= mouseTarget then
			BreakSelectionBox.Adornee = nil
			selectedBlock = nil
			mouse.TargetFilter = mouseTarget
		end
	end

	local distance = (mouseTarget.Position - character.PrimaryPart.Position).Magnitude

	if distance <= breakRange then
		isMouseTargetValid = true

		if selectedBlock ~= mouseTarget then
			BreakSelectionBox.Adornee = nil
		end

		selectedBlock = mouseTarget
		BreakSelectionBox.Adornee = mouseTarget
	else
		BreakSelectionBox.Adornee = nil
		selectedBlock = nil
	end
end)

I tried adding a mouseFilter but it doesn’t work quite well, it work but not good.

How are you setting the tags btw? I assume you’re looping through and tagging all Hitboxes?

I fixed it with this:

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

	for i, v in ipairs(BreakableBlocks) do
		if v == mouseTarget then
			isMouseTargetValid = true
			break
		end
	end

	if isMouseTargetValid then
		local distance = (mouseTarget.Position - character.PrimaryPart.Position).Magnitude

		if distance <= breakRange then
			if mouseTarget ~= selectedBlock then
				isMouseTargetValid = true

				if selectedBlock ~= mouseTarget then
					BreakSelectionBox.Adornee = nil
				end

				selectedBlock = mouseTarget
				BreakSelectionBox.Adornee = mouseTarget
			end
		else
			BreakSelectionBox.Adornee = nil
			selectedBlock = nil
		end
	else
		BreakSelectionBox.Adornee = nil
		selectedBlock = nil
	end
end)
1 Like

Awesome, I was going to suggest a new method which would involve needing to put all your Breakables into a folder. The script takes the Mouse.Target then loops back through each parent until it finds the Model inside the Breakables Folder.

This probably isn’t what you need but if you want to check it out feel free:

local RunService = game:GetService("RunService");
local Players = game:GetService("Players");

local BreakableBlocks = workspace:WaitForChild("Blocks"):GetChildren();
local Player = Players.LocalPlayer
local mouse = Player:GetMouse();

local breakRange = 10
local BreakSelectionBox = Instance.new("SelectionBox",Player.PlayerGui);
local selectedBlock

local function checkIfMinable(block)
	repeat block = block.Parent until(not block or not block.Parent) or block.Parent == BreakableBlocks
	return block
end

local function setAdornee(Value)
	BreakSelectionBox.Adornee = Value
	selectedBlock = Value
end

RunService.RenderStepped:Connect(function()
	local mouseTarget = mouse.Target
	if not mouseTarget then
		return setAdornee(nil)
	end
	local character = Player.Character;
	if not character then
		return setAdornee(nil)
	end
	if not checkIfMinable(mouseTarget) then
		return setAdornee(nil)
	end

	local distance = (mouseTarget.Position - character.PrimaryPart.Position).Magnitude
	if distance <= breakRange then
		setAdornee(mouseTarget)
	else
		setAdornee(nil)
	end
end)

Personally I don’t like using repeat loops as they feel messy lol

3 Likes

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