Distance or percent crossed (part)

If my start pos is:

Vector3.new(0, 0.5, -132)

and my end pos is:

Vector3.new(0, 0.5, -190)

How would i calculate the amount of meters left to walk or the 50% passed? or for example 10/100 meters

I’m not completely sure, but I think what you would do is:

local amountLeft = (Vector3.new(0, 0.5, -132) - Vector3.new(0, 0.5, -190)).Magnitude

Edit: Just tested this in studio, and it returned 58.

Also, if you want to get the percentage left, you would use:

local percentage = math.floor(amountLeft / goal * 100)

what is the goal variable set to

the amount needed to walk to complete the task

well how do i calculate that?

extra text

Could you provide a snippet of your code? Specifically the part where you need to calculate the distance left.

i havent actually made the actual code yet i just made 2 parts and put then far from each other for a test

You have to have a goal to calculate the percentage.

Edit: For your case, you would probably need to use:

goalPart.Position.Magnitude

the goal pos is Vector3.new(0, 0.5, -190)

Then you should probably use:

goalPart.Position.Magnitude

but how would this work bc if i used

Vector3.new(0,0,0)

and the end pos

then it would be a easy equation (10/100) for the dist but for something like

start: Vector3.new(0, 0.5, -132)

end: Vector3.new(0, 0.5, -190)

do i need to position the start part at XYZ 0,0,0?

local percentage = math.floor(((Vector3.new(0,0.5,-132) - Vector3.new(0,0.5,-190))).Magnitude / Vector3.new(0,0.5,-190).Magnitude * 100)

Not completely sure if this works

So for your case, if you want to calulate maybe the Distance of a Player between two points, you need to first have a start, and an end, for example, lets say that the starting point is at Vector3.zero, or 0,0,0, and out end is at Vector3.one*10 or 10,10,10, like here:

p0 = Vector3.zero
p1 = Vector3.one*10

When doing this, you first nee to know the exact distance between p0 and p1 in order to locate p, which is our character, but how do you do this? In order to understand how to get the Distance, you first nee to understand the Pythagorean Theorem and how it works.

The Pythagorean Theorem is a formula used to calculate a distance between p0 and p1, in our case, the starting and end position, the formula for 2D spaces would be sqrt(a² + b²), but for 3D spaces, its sqrt(a² + b² + c²). If you couldn’t tell or dont know:
a = x
b = y
c = z
It should be simple if you understand what squaring (x^2 or x²), and what the square root is, but Vector3, and Vector2 have a Property that allows you to easily get the Distance between points, called Magnitude.
Using this, you can calculate the exact distance between two points, and its with this that you can calulate the distance between other things, I’ll explain how.

As already started, p0 is starting point, and p1 is the location we are trying to get to, but in order to find the location of, we nee to figure out the location of our p, for our full distance, its we subtract p0, and p1, to make our position relative to each other, which is how we get our distance between p0 and p1, with p however its slightly more complicated, when you get closer to the location, the Distance between you and p1 will decrease, if we use the exact same logic for the percentage, it will decrease from 100% down to 0% or less, so what we will do is first get the distance between p and p1, use the full distance between p0 and p1, and then subtract it with p, like so:

local difference = fullDistance - (hrp.Position - p1).Magnitude
-- fullDistance is our complete distance from p0 and p1
-- the math behind is explained below, but as a side note:
-- full distance can be calulated using the following code, 
-- the math was explained earlier

local fullDistance = (p0 - p1).Magnitude

Now, How does this math work? What we are doing is taking away from our full distance using our current distance, when we do this and get closer to our target, our number will instead get bigger, because the amount we are taking away is getting smaller, which will return our distance from p1, perfect!

So using the Data collected from this, we can then calculate the percentage of the distances, the math to calulate this is the following formula: floor(100(x/y))
To Explain it, we first floor (Round down) our number divided by the target number, if you have 0/1, you have no number going into 1, so out number would be 0, if we have 0.5/1, we have half of 1 going into 1, which is 50%, if you have 1/1, it will return 1, as 1 goes into 1 completely, with no remainders, we then multiply this value by 100 to give us a percentage value instead of a decimal value, in code, would look like this:

percentage = math.floor((x/y)*100)

so with our current code, it should look like this:

percentage = math.floor((difference/fullDistance)*100)

And that is how you get the Percentage of the Distance of something!

For example if my start was

Vector3.new(10,0,0)

And my end was

Vector3.new(50,0,0)

Could I just do (end - start).Magnitude for the full dist to travel and then so see the amount I travelled I’d do

End - (HumanoidRootPart.Position - end).Magnitude

Thats basically what I said to do.