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

How To Add Stud Distance To A Script?

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

✓ Accepted answer

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

2 comments

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?

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

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

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