Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to detect range between blocks?

Asked by 10 years ago

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

Log in to vote
3
Answered by 10 years ago

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
Ad
Log in to vote
-1
Answered by
RubenKan 3615 Moderation Voter Administrator Community Moderator
10 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

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)

Answer this question