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

How To Add Stud Distance To A Script?

Asked by 7 years ago
Edited 7 years ago

So here is a script that enables the player to click and drag a unioned part. What is necessary to add to this to make sure players are not able to drag the part from a certain distance?

plr = game.Players.LocalPlayer
mouse = plr:GetMouse()
target = {}

script.Parent = game.StarterPlayer.StarterPlayerScripts
function click()
    if mouse.Target then
        if mouse.Target:IsA("UnionOperation") and mouse.Target.Locked == false then
            target.part = mouse.Target
            target.anchored = target.part.Anchored
            target.part.Anchored = true
            dim = true
            mouse.Move:connect(function()

                if dim then
                    target.part.CFrame = CFrame.new(
                        mouse.Hit.X,target.part.CFrame.Y,mouse.Hit.Z)
                elseif not dim then
                end

            end
            )
            mouse.Button1Up:connect(function()
                dim = false
                 target.part.Anchored = target.anchored

            end)
        end
    end
end
mouse.Button1Down:connect(click)

2 answers

Log in to vote
0
Answered by 7 years ago

Hi Bunny!

The correct operation for finding the distance between two parts is this:

local distance = (partA.Position - partB.Position).magnitude

Measured in studs. This method also works for Vector3's for future reference.

The most encouraged way, at least from me, is that you find the distance between your character's torso and the target block. The easiest way I see is to place it within your second conditional as I do here:

if mouse.Target:IsA("UnionOperation") and mouse.Target.Locked == false and ((plr.Character:WaitForChild('Torso').Postion - mouse.Target.Position).magnitude < 10) then

Keep on coding! ~ HollowMarofan

0
Thank you for the help! I inserted it but it says that position is not a valid member of a part. How do I resolve that problem? BunnyFilms1 297 — 7y
0
Never mind! It was a typo! I got the script working and it is truly amazing! Thank you for helping me with this! Have a great day! :D BunnyFilms1 297 — 7y
Ad
Log in to vote
0
Answered by 7 years ago

In the properties of the Click Detector, you should see " Max Activation Distance. You can change it.

Answer this question