Why is my distance script not working?

I am wanting to make a script that makes a gui that looks like such

My script is not erroring but is making the position not accurate at all.

local Player = game.Players.LocalPlayer
local rs = game:GetService("RunService")
local HRP = script.Parent:WaitForChild("HumanoidRootPart")
local Frame = Player.PlayerGui.ProgressBarUI.ProgessBar
game.Workspace:WaitForChild("AllDoors") game.Workspace.AllDoors:WaitForChild("Final")
local End = game.Workspace.AllDoors.Final.Ground
local image = script.ImageLabel:Clone()
image.Parent = Player.PlayerGui.ProgressBarUI.ProgessBar
	local x = (HRP.Position - End.Position).Magnitude
	local h = (HRP.Position - game.Workspace.SpawnLocation.Position).Magnitude
	local r = Frame.Size
	image.Position = UDim2.new(x/h,0,Frame.Size.Y.Scale /2,0)

The position script is in the heartbeat function.

Iā€™m not too sure what all of your variables represent but you should be using magnitude on the start and the end, and then on the root part and the end to get the progress. So something like

local totalDistance = (startPart.Position - endPart.Position).Magnitude
-- now, to get the distance between the root and the end part:
local currentDistance = (rootPart.Position - endPart.Position)
-- and to get the progress, you'd do:
image.Position = UDim2.fromScale(currentDistance / totalDistance, frame.Size.Y.Scale)
1 Like