Distance & Touching

Greetings.

So I have been playing around with touched and magnitude and I am trying to see if there is a way to make it detect if the player is touching the part from the distance they are away. So basically a range of 0.

Would it work if I did something like that. when the person is physically on the part does the range print 0.

That only works with spheres, because the surface of a sphere is defined as the set of points that are a constant distance (the radius of the sphere) away from the center. The distance between a point and the nearest point on e.g. a cube will depend on the orientation of the cube and the specific point that is being considered.

If you want to find the distance to a pretty close point on the surface of a Part, you can cast a Ray to the center of the cube, since the raycasting methods return the point of intersection between the ray and the Part. If you want the exact distance, you’ll have to program your own functions for doing so.

1 Like

Hey
We use Magnitude to calculate distances , for example :

We want to calculate the difference in distance between part 1 and part2
Here is how we would incorporate Magnitude in to our script

Here the difference was printed

image

We run a script (magnitude works with parts too) [Just an example]

This should work…

local players1 = game:GetService("Players")

local players = players1:GetPlayers()

local part = workspace.Part1

    while true do
	  wait(1)
	  
	for i,v in pairs(players) do
		
		  if not v~=nil and v.Character~=nil
	
	   then 
			 print("No char. found")
    repeat
    
    v:WaitForChild("Character")

    until

    v:FindFirstChild("Character")
			
			 return print("player doesnt exist ") 
			
		end
		
			local pos = v.Character:FindFirstChild('Torso')	
			  if pos and (pos.Position-part.Position).magnitude <= 100 then

		print(v.Name .. " is near the object ")
		
	     end
	   end
      end

And to get the distance here (magnitude)

	local part = workspace.Part
		
		 local part2 = workspace.Part2
   
 local magnitude = (part.Position - part2.Position).magnitude
 print(magnitude)