Destroy part when player clicks it using tool at a certain stud limit

How can I make a tool destroy a part when the player clicks it when they are near enough to it? I am using localscript for this that will replicate to the server using remotes.

I do not know the way of your interaction with the “part” and thus I am showing the mouse target method in my code -

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Mouse = Player:GetMouse()

local part = workspace.Part
local HRP = Character:WaitForChild("HumanoidRootPart")

local maxDistance = 30  -- Customize it

local function onClicked()
	if (Mouse.Target == part) then
		if ((part.Position - HRP.Position).Magnitude > maxDistance) then
			return 
		end
		part:Destroy()
	end
end

Mouse.Button1Down:Connect(onClicked)
1 Like

Based on your indications, I would suggest using the Mouse of the player once the player equips the tool and send remote events to the server once the tool is activated. Send the Mouse.target instance as a parameter to the remote and do your checks on the server.

In the server, you can check if the part parameter is indeed a part or if it isnt nil.
Afterward, you might wanna check if it can be deleted or not, this is your choice, but an example would be: If part.Locked is true, then you can’t delete it.
Lastly, distance check. You can simply check if the distance is too far with something like:

If (part.Position - playerHrp.Position).Magnitude < maxDistance then
    --destroy
end

If this isn’t enough or isn’t what you’re looking for, tell me, I’d like to help.