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

How to detect range between blocks?

I am making a dig tool, and I need to find a way to limit how far away you can dig. I would like it to detect how far away the block I'm looking at is, and if it is more than 5 metres, not let you destroy it. I am not looking for a whole script, simply a way to find the distance. Can anyone help?

2 answers

You can find the length of a vector by using the magnitude property of Vector3 objects. Simply create a new vector between the two vectors that you have, like so:

x = Vector3.new(0, 0, 0)
y = Vector3.new(10, 10, 10)
new_vector = x - y

Then, find the length of it:

print(new_vector.magnitude)

So, we can apply this to your tool like so:

tool = script.Parent
handle = tool.Handle

-- (add your script here)

-- this code in the dig function, assuming `block` is the part they clicked on:
if (block.Position - handle.Position).magnitude < 100 then -- 100 is max distance
	-- dig here
end

localscript inside backpack:

mouse = game.Players.LocalPlayer:GetMouse()
mouse.Button1Down:connect(function()

if (m.Target.Position - game.Players.LocalPlayer.Character.Torso.Position).Magnitude < 5 then -- change 5 to your distance
m.Target:Destroy()
end
end)

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