Convert position (vector3) to percent

I have 2 points A and B and I would like to calculate the percent the player has walked to point B however if the position of the player at the start line is x 10 and the position of the point B is 100 this would glitch and add 10% to the start UI

What you could do is:

-- If this is in PlayerGui, this is the way to reference the character:
local PlayersService = game:GetService("Players")
local player = PlayersService.LocalPlayer

local character = player.Character or player.CharacterAdded:Wait()
local humRootPart = character:WaitForChild("HumanoidRootPart")

-- variables for the vectors
local startPosition = Vector3.new(10, 0, 0)
local endPosition = Vector3.new(100, 0, 0)

local totalDistance = (endPosition - startPosition).Magnitude
-- Magnitude is used to return a single number as a distance for 3d instances.

local function getTravelledDistancePercent()
	local travelledDistance = (humRootPart.Position - startPosition).Magnitude
	-- Finds out the distance between the start and the character.

	return (travelledDistance / totalDistance) * 100
	-- or, if you just want the decimal, remove the * 100
end

Feel free to ask any questions about how the code works!

1 Like

the problem is that the player is beginning at 10 but should be at 0 but you cannot move a part where i placed it so do i need to remove 10 percent from the value?

So, the player starts at x = 10, but their x should equal 0. And when their x starts at 10, their percentage is 10% instead of 0?

yes bc point a is at -19.5 should will make it more difficult to calculate bc normal percentages are calculated 0/10 or 5/10 but this one start at -19.5 not 0

full = (pointX - pointY).Magnitude 
-- our full distance between point X to Point Y

point = full - math.max(0, (player - pointY).Magnitude) -- the amount we traversed (player to point Y)
-- the closer we get, the smaller our distance will be
-- if I was 10 studs away, and point X was 20 studs away
-- 20 - 10 would be 10, meaning I traveled 50% of the way
-- so we subtract the full amount with our current amount
-- and set a limit for how much the number will go, as we dont want
-- a negative percentage, math.max will help with that
-- preventing our number from going lower than 0.
Percentage = math.floor((point/full)*100) -- Conversion to Percentage
-- our point will be from between 0 and 1, multiplying it by 100 will give us
-- 0 to 100, math.floor is to give us a estimate of that percentage

Keep in mind this will change depending on where you are, so if you want it to be based on the direction what are going in I believe you use Dot Products for this purpose, which Im not well educated for that kind of math or code work.

If you are wondering on how exactly the Distance is calulated, I would Recommend this, which should tell you about that.
I made a post about this a while ago, if you want to look at that

If you use my code in my original reply, your problem should be resolved, because instead of directly dividing the point a position by the point b position, it takes the distance between the a position and the character, then divides that by the distance between the a position and b position.